8

What happens if all threads are busy and main thread has sent thread cond signal ?

1 Main Thread and 3 pthreads in thread pool. 3 pthreads are in status of

    pthread_mutex_lock(&sync_mutex);
    pthread_cond_wait(&sync_cond, &sync_mutex);
    pthread_mutex_unlock(&sync_mutex);

Main thread has sent Signal to wake up the threads to process the work. In this situation, What if 3 threads are already busy and next signal has arrived?

Jae Park
  • 621
  • 4
  • 13
  • 27

2 Answers2

11

Nothing. The signal disappears.

zvrba
  • 24,186
  • 3
  • 55
  • 65
  • Thank you, then to avoid this do you have any suggestion to solve this problem? – Jae Park Mar 07 '12 at 09:08
  • 2
    If you want to record the times/count of signals even if there is nothing waiting yet, use a semaphore. Semaphores have a count, condvars events etc. do not. – Martin James Mar 07 '12 at 09:14
  • Semaphores.. I'll check on it thanks. what about using a simple queue to store the events and dequeue from it when Thread gets available? – Jae Park Mar 07 '12 at 09:17
  • 1
    @Jae: a condition variable **signals a change in condition**. The receiving threads must wait for a condition variable until a condition is met, which when using queues means wait until the queue is not empty (see http://stackoverflow.com/a/5538447/104774). – stefaanv Mar 07 '12 at 10:09
4

If your using one of the following functions:

pthread_cond_signal - restarts one of the threads that are waiting on the condition variable cond. pthread_cond_broadcast - wake up all threads blocked by the specified condition variable.

The manual states that

The pthread_cond_broadcast() and pthread_cond_signal() functions shall have no effect if there are no threads currently blocked on cond.

bruno
  • 2,802
  • 1
  • 23
  • 23