MultiThreading
|
Some thread A has processed an incoming trigger and wants to inform a waiting thread B to continue with his part of the work.
There is a condition variable accessible from both threads (e.g. the variable may be a class attribute and the threads are working with the same class instance):
Thread A finishes his part of the work and informs the waiting thread B to start his action:
Thread B waits until he receives the signal to continue with processing:
Attention: This is unsafe code!
The code snippets above will work in many cases and most of the time. But you may have sporadic erraneous behaviour because of the following reasons:
Because of these restrictions you cannot use the condition variable alone for proper synchronization. You always have to set and check your specific data when a decision is needed.
The condition variable is only a trigger to repeat your checks. The meaning of that trigger is: "Now it's a good time to recheck your conditions. But be prepared that they are still not fulfilled".
The next section will describe a safe way of synchronizing with the help of condition variables.