0

I write simple pthread example using condition to sync two threads. in some situation signal called before wait and wait-thread lock forever.

Is there way to detect signal before wait ?

void *Thread1(void *args){
    sleep(1);
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond,&mutex);
    pthread_mutex_unlock(&mutex);
}

void *Thread2(void *args){
    pthread_mutex_lock(&mutex);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
}
MohsenTi
  • 361
  • 1
  • 2
  • 17
  • Use pthread_cond_timedwait to avoid waiting forever? – Sven Nilsson Mar 07 '16 at 14:54
  • I using condition to process a list of jobs and can't calculate waiting time but your idea is good. I can save state of job and use pthread_cond_timedwait in a while, is this latest choice ? – MohsenTi Mar 07 '16 at 15:00
  • 1
    Possible duplicate of [What is the best solution to pause and resume pthreads?](http://stackoverflow.com/questions/13662689/what-is-the-best-solution-to-pause-and-resume-pthreads) – pilcrow Mar 07 '16 at 15:08
  • thank you pilcrow for your attention but no. – MohsenTi Mar 07 '16 at 15:15
  • @MohsenTi, I agree that question looks dissimilar at first glance. However, what I think you're asking is, "how do I correctly use condition variables to pause a thread until another thread says 'OK', skipping that pause if things are already 'OK'?" The chief insight there, as here, is that you need a separate predicate protected by the mutex and checked in a loop. – pilcrow Mar 07 '16 at 15:23
  • Umm, don't wait for something that has already happened? – David Schwartz Mar 08 '16 at 01:24

2 Answers2

4

Stop using condition variable as signal mechanism, as they are not. You always need an actual variable which holds the data. When you need to use the data, your first check it's state, and if the state is not what you want it to be, you wait for condition to happen. Once you are woken, you check the state again.

You can't use condition variables as signals.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
2

Condition variables must always be paired with a condition over some shared state (called a predicate). A simple example is a shared flag variable:

int should_wait = 1;

void *Thread1(void *args)
{
    pthread_mutex_lock(&mutex);
    while (should_wait)
        pthread_cond_wait(&cond, &mutex);
    pthread_mutex_unlock(&mutex);
}

void *Thread2(void *args){
    pthread_mutex_lock(&mutex);
    should_wait = 0;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
}
caf
  • 233,326
  • 40
  • 323
  • 462
  • @MohsenTi: Because `pthread_cond_wait()` is allowed to return early even when it hasn't been signalled (a "spurious wakeup"). – caf Mar 08 '16 at 11:08