It seems very unlikely that all threads will actually stop, although I should point out that in order to avoid false awakenings, you probably should have a while loop instead of an if statement:
lock (waitAck) { while(queryProducer.secondQueue.Count > 0) { Monitor.Wait(waitAck); } }
The fact that you call Monitor.Wait means that waitAck should be released, so it should not block user threads ...
Could you give more information on how producer / consumer flows βstopβ? Looks like they just hit a dead end?
Is your manufacturer used with Notify or NotifyAll ? Now you have an extra wait thread, so if you use only Notify , it is only going to release one thread ... it's hard to see if there is a problem without the details of your Producer and Consumer classes.
If you can show a short but complete program to demonstrate the problem, this will help.
EDIT: Ok, now you have posted the code, and I see a number of problems:
Having a large number of public variables is a recipe for disaster. Your classes must encapsulate their functionality so that no other code needs to be shaken to implement bits and parts. (For example, your calling code here really should not have access to the queue.)
You add items directly to the second line, which means that you cannot effectively wake the manufacturer to add them first. Why do you even have several queues?
You are always waiting for _sync in the producer thread ... why? Where to start to notify? Generally speaking, a producer thread should not wait unless you have a limited buffer
You have a static variable (_waitAck) that is overwritten every time you create a new instance. It is a bad idea.
You also did not show your SyncEvents class - does that mean doing something interesting?
Honestly, it looks like you have a rather strange design - it might well be best for you to start from scratch. Try encapsulating the entire producer / consumer queue into one class that has the Produce and Consume methods, as well as WaitForEmpty (or something like that). I think you will find the synchronization logic a lot easier.
Jon skeet
source share