hung pthread_cond_timedwait on uCLinux with v2linux

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
hi folks,

I have an application running on ARM946 with uClinux OS and Montavista
A thread performs a semaphore take (semTake) with timeout which
v2linux code implements (with conditional variable) as a call to
pthread_cond_timedwait. This call gets hung, I do not get a timeout
(ETIMEDOUT) or any other return code. It seems that the thread stack
got messed up somehow. If I change the call to a semTake(FOREVER) this
does not occure (i.e. the thread lives). If I replace the
pthread_cond_timedwait call with a delay of several seconds it does
not occure.
To disable the problem of stack overflow I created a new thread to
test this, here is the code:

unsigned  __TASKCONV testTask(void)
    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    static pthread_cond_t  cond  = PTHREAD_COND_INITIALIZER;

    struct timeval now;
    struct timespec timeout;
    int retcode;

    if (pthread_mutex_lock(&mutex) != 0)
        fprintf(stderr, "\r\n pthread_mutex_lock failed

    while (1)
        if (gettimeofday(&now, NULL) != 0)
            fprintf(stderr, "\r\n gettimeofday failed
        timeout.tv_sec = now.tv_sec + 5;
        timeout.tv_nsec = now.tv_usec * 1000;

        fprintf(stderr, "\r\n before pthread_cond_timedwait");
        retcode = pthread_cond_timedwait(&cond, &mutex, &timeout);
        fprintf(stderr, "\r\n after pthread_cond_timedwait");

        if (retcode == ETIMEDOUT)
            /* timeout occurred */
            fprintf(stderr, "\r\n retcode == ETIMEDOUT");
            /* operate on x and y */
            fprintf(stderr, "\r\n retcode != ETIMEDOUT");


When running this thread I only see the "before
pthread_cond_timedwait" print!
Does anybody know of the same problem I have encountered? What am I
doing wrong?


Re: hung pthread_cond_timedwait on uCLinux with v2linux

there is nothing wrong with pthread_cond_timedwait, but YOU wait so long.
You get the actual time and add 5 seconds. So you wait the seconds since
1970 + 5 seconds, and thats pretty to much :).

Quoted text here. Click to load it

Change to:
Quoted text here. Click to load it

Have a nice day.

Mit freundlichen Gren / Best Regards
Sebastian Haas

Re: hung pthread_cond_timedwait on uCLinux with v2linux
Quoted text here. Click to load it


According to linux man, the pthread_cond_timedwait call expects
absolute time. The base (where abstime=0) is Jan 1970, this meens that
if I do as you suggested the pthread_cond_timedwait will terminate at
once and return a code of ETIMEDOUT without any wait because the
absolute date/hour has allready passed. See the man example...


Site Timeline