I inherited a bit of streaming code, and looking at it, I find structures like this (in the background thread method):
private ManualResetEvent stopEvent = new ManualResetEvent(false);
private void Run_Thread() {
while (!stopEvent.WaitOne(0, true)) {
}
}
There is usually a public or private method Stop(), for example:
public void Stop() {
stopEvent.Set();
bgThread.Join();
}
My question is: what is served using the wait descriptor here? It seems that this is done so that the alarm for stopping is an atomic operation, but I thought that writing to the logical value of the atom anyway. If so, is there a reason to not just use the following:
private void Run_Thread() {
while(!stop) {
}
}
public void Stop() {
stop = true;
bgThread.Join();
}
source
share