PaxToYou
Posts: 3
Joined: Wed Oct 29, 2014 9:46 pm

pthread_cond_timedwait seems exit without wait

Wed Oct 29, 2014 10:02 pm

Dear all,
I'm trying to use the pthread_cond_timedwait but this call seems not to be able to suspend the execution and always ends returning ETIMEDOUT.
I've created the condition with the default value for attributes (pthread_cond_init (&mbx->cond, NULL)) also why seems to be impossible set a different value: which system call shoul I use to set them up?
Many thanks
----
This is the method where the problem is shown:

// SendWaitForever wants to copy the pointer p into the circular buffer which is inside of any Mbx class instance
bool Mbx::SendWaitForever (void* p)
{ static const struct timespec forever = {(__time_t) 60*60*24*365*20, 0}; // 20 years
bool retVal;

if (pthread_mutex_lock (&mutex))
return false;
again:
retVal = circBuf.Put(&p);
if (retVal)
pthread_cond_broadcast (&cond);
else {
int retval = pthread_cond_timedwait (&cond, &mutex, &forever);
char *c = strerror(retval);
bool b = ETIMEDOUT == retval;
goto again;
}
pthread_mutex_unlock (&mutex);
return retVal;
}

PaxToYou
Posts: 3
Joined: Wed Oct 29, 2014 9:46 pm

Re: pthread_cond_timedwait seems exit without wait

Thu Oct 30, 2014 10:56 am

I've found a little bit more of information. The time specified in the call doen't have as origin the moment when the call is done but it is relative to the value returned by the clock_gettime system call. A good example (copied from http://pubs.opengroup.org/onlinepubs/9699919799) is the following:
(void) pthread_mutex_lock(&t.mn);
t.waiters++;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 5;
rc = 0;
while (! mypredicate(&t) && rc == 0)
rc = pthread_cond_timedwait(&t.cond, &t.mn, &ts);
t.waiters--;
if (rc == 0 || mypredicate(&t))
setmystate(&t);
(void) pthread_mutex_unlock(&t.mn);

Return to “Advanced users”