Multi-threaded access to common resources often results in data confusion and unexpected results. Therefore, it is generally necessary to lock access for mutually exclusive access.
The so-called "mutex access" refers to a sectionCodeOr a public variable can be accessed by only one thread at a time. Other threads need to wait until the modification thread completes processing and notifies the next waiting thread for processing.
1. You can use "Lock" to provide mutually exclusive access to the Code:
Lock (a class example, the Class Name of the static variable) {// code}
2. You can use "Monitor" to provide mutex access to variables:
Int money; monitor. Enter (money); // money ++ or money --; monitor. Exit (money );
Monitor maintains two queues. One is the reserve queue column, which stores the thread preparing to obtain the lock. The other is the waiting queue, which stores the thread waiting for the object to change its status. When the lock is released by a thread, call the monitor. Pulse () method to put the first thread in the waiting queue in the reserve queue column.
Here is an example of using lock and monitor:
Using system; using system. collections. generic; using system. text; using system. threading; namespace demo {public class productclass {int products; bool enableget = false; // If ture, consumer can get product, otherwise producer is producing. public int getproducts () {lock (this) {If (! Enableget) {try {Monitor. wait (this);} catch (synchronizationlockexception ex) {console. writeline ("[synchronizationlockexception Error]:" + ex. tostring ();} catch (threadinterruptedexception ex) {console. writeline ("[threadinterruptedexception Error]:" + ex. tostring () ;}} console. writeline ("consume: {0}", products); // reset to be false after getting products. enableget = false; // producer y producing thread to produce products monitor. pulse (this);} return products;} public void produceproducts (INT products) {lock (this) {If (enableget) {try {Monitor. wait (this);} catch (synchronizationlockexception ex) {console. writeline ("[synchronizationlockexception Error]:" + ex. tostring ();} catch (threadinterruptedexception ex) {console. writeline ("[threadinterruptedexception Error]:" + ex. tostring () ;}} this. products = products; console. writeline ("produce: {0}", products); enableget = true; monitor. pulse (this) ;}} public class producer {productclass aproductclass; int producetimes = 1; Public producer (productclass aproductclass, int producetimes) {This. aproductclass = aproductclass; this. producetimes = producetimes;} public void threadrun () {for (INT I = 0; I <producetimes; I ++) {aproductclass. produceproducts (I) ;}} public class Consumer {productclass aproductclass; int getproducttimes = 1; public consumer (productclass aproductclass, int getproducttimes) {This. aproductclass = aproductclass; this. getproducttimes = getproducttimes;} public void threadrun () {int returnvalue; For (INT I = 0; I <getproducttimes; I ++) {returnvalue = aproductclass. getproducts () ;}} class program {static void main (string [] ARGs) {productclass aproductclass = new productclass (); Producer aproducer = new producer (aproductclass, 20 ); consumer Aconsumer = new consumer (aproductclass, 20); thread producethread = new thread (New threadstart (aproducer. threadrun); thread consumethread = new thread (New threadstart (Aconsumer. threadrun); try {producethread. start (); consumethread. start (); producethread. join (); consumethread. join (); console. readline ();} catch (threadstateexception ex) {console. writeline ("[threadstateexception Error]:" + ex. tostring ();} catch (threadinterruptedexception ex) {console. writeline ("[threadinterruptedexception Error]:" + ex. tostring ());}}}}