C # multithreading autoresetevent and manualresetevent

Source: Internet
Author: User

Sometimes, in specific development, we need to design the program into a multi-threaded logic.

We simulate this scenario: (C/S Mode)

1. The client sends a measurement command to the server.

2. The server receives measurement commands from the client.

3. The server performs measurement.

4. The server returns the measurement result to the client.

The above four steps are a complete interaction process. When we require the server to have multiple client test commands at the same time, we need to use the multi-threaded design: Create a thread for each client to execute the above four steps, and other client threads do not interfere with each other.

In the case of multiple threads, the execution sequence of each thread is parallel. But sometimes we need to control multiple threads so that they can be executed in a specific order, for example, when we add a 'prioritized 'to the client in the preceding scenario.

Here we will explain how to use autoresetevent and manualresetevent to control the execution of multiple threads.

1. In simple mode, we create three threads, T1, T2, and T3, and print the ID of each thread in sequence for 10 times. (This may not happen in reality, but it is used to demonstrate how to control multithreading)

Solution A: autoresetevent implementation:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApplication14{    class Program    {        static AutoResetEvent are1 = new AutoResetEvent(true);        static AutoResetEvent are2 = new AutoResetEvent(false);        static AutoResetEvent are3 = new AutoResetEvent(false);        static void Main(string[] args)        {                          Thread t1 = new Thread(new ThreadStart(RunFun1));            t1.Start();                        Thread t2 = new Thread(new ThreadStart(RunFun2));            t2.Start();                        Thread t3 = new Thread(new ThreadStart(RunFun3));            t3.Start();                        Console.Read();        }        static void RunFun1()        {            for (int i = 0; i < 10; i++)            {                are1.WaitOne();                Console.WriteLine("The " + (i+1) + " times running:");                Console.WriteLine("Thread t1 ID:" + Thread.CurrentThread.ManagedThreadId);                              are2.Set();            }        }        static void RunFun2()        {            for (int i = 0; i < 10; i++)            {                are2.WaitOne();                Console.WriteLine("Thread t2 ID:" + Thread.CurrentThread.ManagedThreadId);                               are3.Set();                            }        }        static void RunFun3()        {            for (int i = 0; i < 10; i++)            {                are3.WaitOne();                Console.WriteLine("Thread t3 ID:" + Thread.CurrentThread.ManagedThreadId);                Console.WriteLine();                are1.Set();            }        }          }}

 

Solution B: manualresetevent implementation:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApplication15{    class Program    {        static ManualResetEvent are1 = new ManualResetEvent(true);        static ManualResetEvent are2 = new ManualResetEvent(false);        static ManualResetEvent are3 = new ManualResetEvent(false);        static void Main(string[] args)        {            Thread t1 = new Thread(new ThreadStart(RunFun1));            t1.Start();            Thread t2 = new Thread(new ThreadStart(RunFun2));            t2.Start();            Thread t3 = new Thread(new ThreadStart(RunFun3));            t3.Start();            Console.Read();        }        static void RunFun1()        {            for (int i = 0; i < 10; i++)            {                are1.WaitOne();                Console.WriteLine("The " + (i + 1) + " times running:");                Console.WriteLine("Thread t1 ID:" + Thread.CurrentThread.ManagedThreadId);                ResetAll();                are2.Set();            }        }        static void RunFun2()        {            for (int i = 0; i < 10; i++)            {                are2.WaitOne();                Console.WriteLine("Thread t2 ID:" + Thread.CurrentThread.ManagedThreadId);                ResetAll();                are3.Set();            }        }        static void RunFun3()        {            for (int i = 0; i < 10; i++)            {                are3.WaitOne();                Console.WriteLine("Thread t3 ID:" + Thread.CurrentThread.ManagedThreadId);                Console.WriteLine();                ResetAll();                are1.Set();            }        }        static void ResetAll()        {            are1.Reset();            are2.Reset();            are3.Reset();        }    }}

The use of autoresetevent and manualresetevent is basically the same. The difference is that after the former is set, only one thread can be allowed and only one time can be passed. The latter allows multiple threads to pass through multiple times until manual reset does not allow threads to pass through.

The basic idea of the above Code is: assign a semaphore of your own to each thread. Each thread must request its own semaphore before execution. After execution, the semaphore to release another thread (BOTH: wake up another thread ).

Every time the auresetevent variable is set, after waitone is executed, it will automatically return to the non-signal state without calling reset. However, manualresetevent is required.

 

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.