Does EventWaitHandle have to deal with side effects?
Note. I am limited to .NET 3.5, so I cannot use ManualResetEventSlim
.
I need to deal with Spurious wakeups when you do something like this:
var waitHandle = new EventWaitHandle();
new Thread(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(5));
waitHandler.Set();
});
waitHandle.WaitOne();
If so, set the correct memory barrier on call Set
and / or WaitOne
to keep it safe:
var reallyDone = false;
var waitHandle = new EventWaitHandle();
new Thread(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(5));
reallyDone = true;
waitHandler.Set();
});
while (!reallyDone)
waitHandle.WaitOne();
In particular, is it possible that the main thread in this example might not notice that the parameter is reallyDone
set to true due to command reordering or caching? Can it reallyDone
be unstable in this case or is it unnecessary?
source to share
There are no side awakenings for events (MRE, ARE and subtle versions). Almost all Windows programs would break if there were such things with these objects. You are fighting windmills. But yes, many synchronization functions, including wait and setup events, fulfill a complete memory barrier (which is well understood, but not documented anywhere). Variable conditions to allow false wakes (like document state). They are not event related.
Also, why did collateral awakenings occur? Doesn't make sense from an API point of view. The event can simply loop inside and hide the side wakes from you (indeed, MRESlim does this). I can only repeat: almost all programs have broken. This is not reality.
Blocks the current thread until the current WaitHandle receives a signal. The caller of this method will block indefinitely until the current instance receives a signal.
These claims would be false if false awakenings existed in the context of the events.
You are misinterpreting what you have seen. You have an error, but it was not caused by an event. reallyDone
No technique required.
source to share