I agree with the comment, which says that you must encode consumer streams in order to accept the possibility of getting the same value several times. Perhaps the easiest way to do this is to add a consistent identifier for each update. In this way, the thread can compare the sequential identifier with the last identifier that it reads, and know if it receives a duplicate.
He would also know if he missed the meaning.
But if you really need them to be in lock mode and only get a value once, I would suggest that you use two objects, ManualResetEvent and CountdownEvent . Here's how to use them.
ManualResetEvent DataReadyEvent = new ManualResetEvent(); ManualResetEvent WaitForResultEvent = new ManualResetEvent(); CountdownEvent Acknowledgement = new CountdownEvent(NumWaitingThreads);
Reader threads are waiting on a DataReadyEvent .
When another thread reads a value from the network, it does this:
Acknowledgement.Reset(NumWaitingThreads); DataReadyEvent.Set(); // signal waiting threads to process Acknowledgement.WaitOne(); // wait for all threads to signal they got it. DataReadyEvent.Reset(); // block threads' reading WaitForResultEvent.Set(); // tell threads they can continue
Pending threads do this:
DataReadyEvent.WaitOne(); // wait for value to be available // read the value Acknowledgement.Set(); // acknowledge receipt WaitForResultEvent.WaitOne(); // wait for signal to proceed
This has the same effect as two events on the waiting thread, but is much simpler.
This has a drawback, however, that if the flow falls, it will hang in the countdown event. But then your method will also be if the producer thread is expecting all messages from the thread.
source share