Has anybody met the following problem - there is a thread A that should notify another thread B and then wait for a backward notification from the thread B. Something like that:
AutoResetEvent BNotifier = ...;
AutoResetEvent ANotifier = ...;
The problem with the code above is that between 1) and 2) thread A may be preempted and this way 4) will be called before the 2). If we suppose that there are several other threads already waiting for ANotifier, state of the ANotifier may be changed back to non-signaled before the call to 2), and this way Thread A will be waiting forever.
The following code solves this problem in most situations, but it is not reliable, is error-prone and ineffective:
Tast.Factory.StartNew( () =>
Thread.Sleep( 1000 );
Are there any other ways to ensure Thread B to be notified only after the Thread A starts waiting for the ANotifier?