POSIX THREADS : I have two functions that are called using two different thread. In thread am using conditional wait and in other am sending signal condition. Since any of the thread can execute earlier, so signal may be sent even when wait was not called. I want to save the signal that has been sent so that when other thread of same process when calls wait can use that signal that was called earlier. Is ther any way to do so in POSIX?
Asked
Active
Viewed 101 times
2 Answers
1
POSIX condition variable signals get lost if there are no waiters. Also, spurious signals may end wait prematurely. This is why one should always wait for state change in a while
loop, rather than a condition variable signal alone.
For you particular task you can probably use something like a semaphore.

Community
- 1
- 1

Maxim Egorushkin
- 131,725
- 17
- 180
- 271
0
The wait-condition alone is not enough to synchronize the threads. As Maxim already said, signals can get lost or may be sent without reason.
You need additional information to be passed between the threads. This is usually done with a queue. But in simple cases, when only a trigger is needed, an int
as counter may be enough.
That looks roughly like this:
sender:
get_lock();
counter++;
release_lock();
receiver:
while(1){
get_lock();
while(counter==0)
wait_on_lock();
counter--;
release_lock();
do_work();
}

rtlgrmpf
- 431
- 2
- 6