Results 1 to 3 of 3
i All,
I am developing an application which uses timeout for mutexes.( gcc version 4.2.3,glibc -2.7 on 2.6.24 kernel and Ubuntu 8.04 flavor of linux ). and iam using libpthread ...
- 08-21-2008 #1Just Joined!
- Join Date
- Aug 2008
- Posts
- 6
POSIX mutex locking with timeout not working with sub-second resolution.
i All,
I am developing an application which uses timeout for mutexes.(gcc version 4.2.3,glibc -2.7 on 2.6.24 kernel and Ubuntu 8.04 flavor of linux). and iam using libpthread and librt libraries.
Here the timeout is specified using a struct timeval which has 2 variables :tv_sec and tv_nsec.
In my app,The pthread_mutex_timedwait works fine as long as the timeout value is more than or equal to a second.
But if i go for a timeout value in millisecond range(even 500ms),it does not work ...
the POSIX spec says
" pthread_mutex_timedlock - lock a mutex (ADVANCED REALTIME)
The timeout shall expire when the absolute time specified by abs_timeout passes, as measured by the clock on which timeouts are based (that is, when the value of that clock equals or exceeds abs_timeout), or if the absolute time specified by abs_timeout has already been passed at the time of the call.
[TMR] If the Timers option is supported, the timeout shall be based on the CLOCK_REALTIME clock; if the Timers option is not supported, the timeout shall be based on the system clock as returned by the time() function.
Is the timer option not enabled for me ? if so, how can i enable this ? Is there anything i have missed ?
Any help on this is deeply appreciated.
P.S nanosleep() which takes the same timespec struct works with 10ms resolution also.
- 08-21-2008 #2This may seem silly, but would you please post the code with which you set the struct timeval to 500 ms?But if i go for a timeout value in millisecond range(even 500ms),it does not work--
Bill
Old age and treachery will overcome youth and skill.
- 08-21-2008 #3Just Joined!
- Join Date
- Aug 2008
- Posts
- 6
Hi wje_lf,
Thanks for the quick response.
Anyways, i found the solution to the problem. Actually,I didn take care of situations where tv_nsec overflows. To illustrate with an eg
previously i was using,
Timeout.tv_sec = Now.tv_sec + (time in sec for timeout);
Timeout.tv_sec = Now.tv_nsec + (time in nsec for timeout);
this was giving erractic output.
when i changed it to following (to take care of overflow),
Timeout.tv_sec = Now.tv_sec + (time in sec for timeout);
Timeout.tv_sec = Now.tv_nsec + (time in nsec for timeout);
if(Timeout.nsec >= 1000000000) //i.e 1 sec
{
++Timeout.sec;
Timeout.nsec -= 1000000000;
}
it worked perfectly.


Reply With Quote