Thread and sync in C # (thread and synchronization in C)

Source: Internet
Author: User

Thread isProgramThe encapsulation of the control flow in. You may have been used to writing single-threaded programs, that is, programs in theirCodeIt is executed only in one path at a time. If you have several more threads, the Code may be more "synchronized ". In a typical process with multiple threads, zero or more threads run simultaneously. However, on a machine with N CPUs, a thread can only run on one CPU at a given time, because each thread is a code segment, and each CPU can only run a code segment at a time. It seems that N concurrent operations are the effect of sharing CPU time slices between threads. In this example, we will create another thread. We will use two threads to demonstrate how multithreading works. Finally, we will implement synchronization between two threads (the main thread and the new thread, wait for the message before the new thread is working. Before creating a thread, we must introduce the system. Threading namespace. What I need to know is that the thread has to set up a starting point for the control process. The start point is a function that can make the same call or other.

Here you can see the starting function defined in the same class.

Code
Using System;
  Using System. Threading;
  Namespace Threadingtester
{
  Class Threadclass
{
Public Static Void Trmain ()
{
For ( Int X =   0 ; X < 10 ; X ++ )
{
Thread. Sleep ( 1000 );
Console. writeline (X );
}
}
Static Void Main ( String [] ARGs)
{
Thread thrd1 =   New Thread ( New Threadstart (trmain ));
Thrd1.start ();
For ( Int X =   0 ; X < 10 ; X ++ )
{
Thread. Sleep ( 900 );
Console. writeline ( " Main: " + X );
}
}
}
}

 

Thread. Sleep (n) method puts the "This" thread in sleep state for N milliseconds. You can take a look at this example. In the main function, we define a new thread, where the starting point is the function trmain (), and then we include the START () method to start execution. If you run this example, you will understand the switching between threads (let the CPU switch from one thread to another) so that the threads can run almost simultaneously, to see which thread is running faster, I set the main thread to be 100 milliseconds less than the new thread.
Now, before starting the thread, name the thread first:
Thread thrd1 = new thread (New threadstart (trmain ));
Thrd1.name = "thread1 ";
Thrd1.start ();
Thread TR = thread. currentthread;
Console. writeline (tr. Name );
After the above program is completed, imagine that we don't want to let it end immediately after a new thread starts. That is to say, we started a new thread to run it at a specific time point, the new thread suspends and waits for messages sent from the main thread (or other threads.
We can define it as follows:
Public static manualresetevent Mre = new manualresetevent (false );
When manualresetevent is created, false is used as the initial state of start. This class is used to notify another thread to wait for one or more threads. Note: To notify or listen to the same thread, all other threads can access that class.
Wait for the thread to write as follows:
Mre. waitone ();
This will cause the waiting thread to be blocked indefinitely and wait for the class to notify.
The signal sending thread should be like this:
Mre. Set ();
In this way, the class will be notified, the value will change to true, and the waiting thread will stop waiting. After a notification event occurs, we can use the following statement to place the thread in the base state:
Mre. Reset ();
Now let's execute the following code in the program:

 

Code
Using System;
  Using System. Threading;
  Namespace Threadingtester
{
  Class Threadclass
{
  Public Static Manualresetevent MRE =   New Manualresetevent ( False );
  Public Static Void Trmain ()
{
Thread tr = Thread. currentthread;
Console. writeline ( " Thread: waiting for an event " );
Mre. waitone ();
Console. writeline ( " Thread: Got an event " );
  For ( Int X =   0 ; X < 10 ; X ++ )
{
Thread. Sleep ( 1000 );
Console. writeline (tr. Name +   " : " + X );
}
}
  Static Void Main ( String [] ARGs)
 
{
Thread thrd1 =   New Thread ( New Threadstart (trmain ));
Thrd1.name =   " Thread1 " ;
Thrd1.start ();
  For ( Int X =   0 ; X < 10 ; X ++ )
{
Thread. Sleep ( 900 );
Console. writeline ( " Main: " + X );
  If ( 5   = X) MRE. Set ();
}
  While (Thrd1.isalive)
{
Thread. Sleep ( 1000 );
Console. writeline ( " Main: waiting for thread to stop " );
}
}
}
}

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.