Thursday 19 May 2011

Threading Locks and Synchronisation

Locks

The usual pattern for locking a piece of code so that other threads cannot enter at the same time is:


Which internally is the same as using the Monitor keyword:


Synchronisation

To cause a thread to block you can use the AutoResetEvent. The WaitOne method will block until the Set method is called:

Or you can use the Monitor class with a lock to achieve the same result:

No comments:

Post a Comment