I understand the basics of threading and how a deadlock occurs. I understand that you want to take out locks in a specific order to help ensure you don't get yourself into a circular firing squad.
What my brain is having trouble with is how that order is guaranteed and exactly how wait and pulse (pulseall) assist.
Don't answer with a link to skeet's article on multithreading as that is where I am coming from. I also googled So and came up with this post.
In Jon's tutorial the methods Produce() and Consume both lock on listlock and I don't see how an order is maintained. Regardless of order one beats the other to the punch so how can the other get a lock? It doesn't right...so the last thread to attempt lock sits there blocking until the first gets to the pulse or the wait depending?
So being unsure I looked for other articles and in the SO article above I took to gatopeich answer with a boss, a worker, and the locked object the phone.
The worker locks the phone while at work. When he has the lock (while)true) he unlocks??? the phone with the monitor.wait statement....???
Meantime the boss locks the phone...but wait how can he the employee got to work on time and locked the phone before the boss got there....?
Jon points out that if you don't understand this you'll think it is going to deadlock which is exactly where I am at:
Just to repeat: calling Wait unlocks the monitor you're waiting on. This is an important point, because otherwise the code looks like it'll just deadlock!
So I am sorry for being so dense and seeing this as a chicken or the egg scenario. Clearly there must be something I am not understanding of how the threads interact when specifying the order of obtaining locks to avoid a deadlock.
Thank You for your help and patience.