I'm having trouble synchronizing threads in C #. I have a shared object that is controlled by two threads, I made access to the object mutually exclusive with lock (), but I also want to block each thread depending on the state of the shared object. Specifically block thread A when the object is empty, block thread B when the object is full, and another thread signals a blocked thread when the state of the object changes.
I tried to do this using ManualResetEvent, but ran into a race condition when thread B detects that the object is full, go to WaitOne and thread A will enter and lower the object (signals MRE about every access and block itself when the object is empty) before than thread A gets into WaitOne, that is, thread A expects that thread will not be full, even if it is not.
I believe that if I could call a function like SignalAndWaitOne that would atomically signal before waiting, would this prevent this race condition?
Thanks!
source share