C # multi-threaded source code instances include lock, monitor, and mutex.

Source: Internet
Author: User

Using system; <br/> using system. collections; <br/> using system. text; <br/> using system. threading; <br/> public class crunmain <br/>{< br/> Public static void main () <br/>{< br/> // syncthread1 (); <br/> // syncthread2 (); <br/> syncthread3 (); <br/>}< br/> Public static void syncthread1 () <br/>{< br/> ctest test = new ctest (1000); <br/> thread trd1 = new thread (New threadstart (test. add); <br/> thread trd2 = new thread (New threadstart (test. sub); <br/> trd1.start (); <br/> trd2.start (); <br/> while (true) <br/>{< br/> console. writeline ("=============================== main thread is running ================ ======= "); <br/> thread. sleep (100); <br/>}< br/> Public static void syncthread2 () <br/>{< br/> ctest2 test = new ctest2 (100); <br/> thread trd1 = new thread (New threadstart (test. add); <br/> thread trd2 = new thread (New threadstart (test. sub); <br/> trd1.start (); <br/> trd2.start (); <br/> while (true) <br/>{< br/> console. writeline ("=============================== main thread is running ================ ======= "); <br/> thread. sleep (100); <br/>}< br/> Public static void syncthread3 () <br/>{< br/> ctest3 test = new ctest3 (10); <br/> thread trd1 = new thread (New threadstart (test. add); <br/> thread trd2 = new thread (New threadstart (test. sub); <br/> trd1.start (); <br/> trd2.start (); <br/> while (true) <br/>{< br/> console. writeline ("=============================== main thread is running ================ ======= "); <br/> thread. sleep (1000); <br/>}< br/> public class ctest3 <br/>{< br/> private mutex m_objmutex; <br/> private int m_ncount = 0; <br/> Public ctest3 (INT nval) <br/>{< br/> m_objmutex = new mutex (); <br/> m_ncount = nval; <br/>}< br/> Public void add () <br/>{< br/> while (true) <br/>{< br/> m_objmutex.waitone (); <br/> m_ncount + = 1; <br/> console. writeline ("after add 1" + "/tncount =" + m_ncount); <br/> m_objmutex.releasemutex (); <br/> If (m_ncount> 0) <br/>{< br/> thread. sleep (1000); <br/>}< br/> Public void sub () <br/>{< br/> while (true) <br/>{< br/> m_objmutex.waitone (); <br/> m_ncount-= 1; <br/> console. writeline ("After sub 1" + "/tncount =" + m_ncount); <br/> m_objmutex.releasemutex (); <br/> If (m_ncount <0) <br/>{< br/> thread. sleep (1000 ); <br/>}</P> <p> public class ctest2 <br/>{< br/> private object m_objlock = new object (); <br/> private int m_ncount = 0; <br/> Public ctest2 (INT nval) <br/>{< br/> m_ncount = nval; <br/>}< br/> Public void add () <br/>{< br/> while (true) <br/>{< br/> monitor. enter (m_objlock); <br/> m_ncount + = 1; <br/> console. writeline ("after add 1" + "/tncount =" + m_ncount); <br/> monitor. exit (m_objlock); <br/> If (m_ncount> 0) <br/>{< br/> thread. sleep (100); <br/>}< br/> Public void sub () <br/>{< br/> while (true) <br/>{< br/> monitor. enter (m_objlock); <br/> m_ncount-= 1; <br/> console. writeline ("After sub 1" + "/tncount =" + m_ncount); <br/> monitor. exit (m_objlock); <br/> If (m_ncount <0) <br/>{< br/> thread. sleep (100 ); <br/>}< br/> public class ctest <br/>{< br/> private object objlock = new object (); <br/> private int m_ncount = 100; <br/> Public ctest (INT nval) <br/>{< br/> m_ncount = nval; <br/>}< br/> Public void add () <br/>{< br/> while (true) <br/>{< br/> lock (objlock) <br/>{< br/> m_ncount + = 1; <br/> console. writeline ("after add 1" + "/tncount =" + m_ncount); <br/>}< br/> If (this. m_ncount> 0) <br/>{< br/> thread. sleep (10); <br/>}< br/> Public void sub () <br/>{< br/> while (true) <br/>{< br/> lock (objlock) <br/>{< br/> m_ncount-= 1; <br/> console. writeline ("After sub 1" + "/tncount =" + m_ncount); <br/>}< br/> If (this. m_ncount <0) <br/>{< br/> thread. sleep (10); <br/>}< br/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.