C # multi-thread programming in the language. It is completely a bachelor's knowledge in OS.

Source: Internet
Author: User

C # multi-thread programming in the language. It is completely a bachelor's knowledge in OS.
Basic knowledge: parameters of the Thread class without parameters and the Thread class with parameters are parameter pointers. You can input a function without parameters. If you want to pass in a function with parameters, first assign a new ParameterizedThreadStart to the instance and use the function name with parameters as its parameter. A function with parameters must have only one object parameter. Refer to the following ConterWithParam (object param) copy code static int iCnt = 0; static readonly int N = 10000; static void Main (string [] args) {Thread [] thrs = new Thread [N]; for (int I = 0; I <N; I ++) {thrs [I] = new Thread (Counter ); thrs [I]. start () ;}for (int I = 0; I <N; I ++) {thrs [I]. join ();} Console. writeLine (iCnt); iCnt = 0; ParameterizedThreadStart [] thrsp = new ParameterizedThreadStart [N]; object param = 2; for (int I = 0; I <N; I ++) {thrsp [I] = new ParameterizedThreadStart (ConterWithParam ); thrs [I] = new Thread (thrsp [I]); thrs [I]. start (param) ;}for (int I = 0; I <N; I ++) {// when NewThread calls the Join method, MainThread is stopped, thrs [I]. join ();} Console. writeLine (iCnt);} static void Counter () {Thread. sleep (100); iCnt ++; // Console. writeLine ("finish iCnt ++, now iCnt is" + iCnt);} static Void ConterWithParam (object param) {Thread. sleep (100); int I = (int) param; iCnt + = I; // Console. writeLine ("finish iCnt +" + I + ", now iCnt is" + iCnt);} copy the code to output the result in this example. 2) check whether the first result of mutex is a bit strange. The result is 10000 when it is added 9550 times. The example with parameters is added 2 at a time and 10000 times. It should be 20000. The result is 19678. This is because the access to iCnt to global variables conflicts. For example, if both threads get the value of iCnt = 0 and execute I ++ at the same time, the final state of iCnt must be 1 rather than 2. To make iCnt get the correct value. We introduce mutex signals. Copy the code static int iCnt = 0; static readonly int N = 10000; static Mutex mut = new Mutex (); static void Main (string [] args) {Thread [] thrs = new Thread [N]; for (int I = 0; I <N; I ++) {thrs [I] = new Thread (Counter ); thrs [I]. start () ;}for (int I = 0; I <N; I ++) {thrs [I]. join ();} Console. writeLine (iCnt); iCnt = 0; ParameterizedThreadStart [] thrsp = new ParameterizedThreadStart [N]; object param = 2; For (int I = 0; I <N; I ++) {thrsp [I] = new ParameterizedThreadStart (ConterWithParam ); thrs [I] = new Thread (thrsp [I]); thrs [I]. start (param) ;}for (int I = 0; I <N; I ++) {// when NewThread calls the Join method, MainThread is stopped, thrs [I]. join ();} Console. writeLine (iCnt);} static void Counter () {Thread. sleep (10); // lock mut. waitOne (); iCnt ++; // release mut. releaseMutex (); // Console. writeLine ("finis H iCnt ++, now iCnt is "+ iCnt);} static void ConterWithParam (object param) {Thread. sleep (10); // lock the mut. waitOne (); int I = (int) param; iCnt + = I; // release mut. releaseMutex (); // Console. writeLine ("finish iCnt +" + I + ", now iCnt is" + iCnt);} copy the code. Result 3) the synchronous signal Semaphore is explained by the most typical producer-consumer. Copy the code static Semaphore sem = new Semaphore (0, 1); static Semaphore sem2 = new Semaphore (0, 5); static void Main (string [] args) {Console. writeLine ("consumer and other producers produce an item"); Thread thConsumer = new Thread (Consume); thConsumer. start (); Thread thProductor = new Thread (Product); thProductor. start (); thConsumer. join (); thProductor. join (); Console. writeLine ("............................. "); Console. writeLine ("Multi-concurrency example ..... "); for (int I = 0; I <10; I ++) {Thread t1 = new Thread (Consume2); t1.Start (); thread t2 = new Thread (Product2); t2.Start () ;}// end for} static void Product () {Thread. sleep (1, 2000); Console. writeLine ("Product an item... "); sem. release ();} static void Consume () {Thread. sleep (1, 1000); Console. writeLine ("Consumer is waiting an item... "); sem. waitOne (); Console. writeLine ("Consumer get an item... ");} static void Product2 () {Thread. sleep (1, 1000); Console. writeLine ("Product an item... "); sem2.Release ();} static void Consume2 () {Thread. sleep (1, 1000); Console. writeLine ("Consumer is waiting an item... "); sem2.WaitOne (); Console. writeLine ("Consumer get an item... ");}

Related Article

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.