I am in a soup. The idea may be bad but i do need a solution.
-
I have two condition variable, say A and B.
-
Threads 1, 2 and 3 are waiting on A. Thread 4 is waiting on B.
-
B will be pthread_cond-signal() by thread 2, that is thread 4 will be
signaled to wake up by thread 2.
Now, I have another thread 5 which pthread_cond_broadcasts() on condition variable A. I need all threads 1, 2 and 3 to wake up before thread 4 wakes up. That is say if thread 2 wakes up and signals on B thread 4 may wake up before thread 3 does, which is not what i want.
Any pointers will be highly appreciated.
thanks
Use a semaphore: have each of threads 1-3 post the semaphore, and have thread 4 wait on the semaphore 3 times instead of on a condition variable.
You’ll want to use
sem_init(3)orsem_open(3)to create the semaphore,sem_post(3)to post the semaphore,sem_wait(3)to wait on the semaphore, and then eithersem_destroy(3)(if created withsem_init) orsem_close(3)andsem_unlink(3)(if created withsem_open) to destroy the semaphore.