Re: function g_cond_timed_wait is not safe when change of datetime?



On Fri, 21 Nov 2008 18:25:12 +0800
"陈杰" <chenj lemote com> wrote:

> Hi, All
> 
> After calling g_get_current_time to get the current timeval, a call to
> *settimeofday* (maybe from another process), just before calling
> g_cond_timed_wait will cause some problem.
> 
> Say, the following code will cause the current process block for very
> long time:

[snip]

You need to use a monotonic clock if you don't want to use the system
one.  The system clock can be altered by a privileged user (for obvious
reasons - you need to be able to set it to the current time every now
and then to accommodate clock drift).

I am not certain if glib condition variables support monotonic clocks,
I suspect not, but if you are using a unix-like system you can mix
pthread calls with GThread calls.  In particular you can use a pthread
mutex and pthread condition variable, and then apply the monotonic
posix functions to that - see pthread_condattr_setclock() and
clock_gettime().

First you will need to check that any target system supports monotonic
clocks (most modern ones do) - see the CLOCK_MONOTONIC and
_POSIX_MONOTONIC_CLOCK manifest constants, and the sysconf() function
(which can be used at run time or compile time).

Probably windows has something similar, but I do not use that OS.

Chris



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]