C # multi-thread synchronization Solution

Source: Internet
Author: User
C # has good support for multi-threaded programming. The following solutions are commonly used:

1. Lock (object) pairs to be synchronizedCodeBlock locking;

2. Monitor class

3. readerwriterlock class

4. mutex class

5. semaphore

6. Event

This time I will mainly talk about lock and monitor. Some experience in multi-thread programming is required for lock.ProgramMembers will be familiar with it, and they will know what the name means. Lock is a lock. For example, it is not elegant. For example, when we go to WC, we will lock the door when you go in. Later people will only wait for you to complete the convenience before entering.

Private   String Lockflag =   " Lock " ;
Public   Void Gowc (person P)
{
Lock (Lockflag)
{
Console. writeline ( " Begin " );
Console. writeline ( " End " );
}
}

 

 

Lock is very convenient to use. One thing to note is that the object you synchronize is preferably private or private static, because if this synchronization object is public or internal, it may be obtained by other parts of the Code in other programs. In this way, your lock will be lost. That is to say, this WC key can only have one, only those who enter WC can get it. Other waiting persons cannot get it.

Another common solution is to use the monitor class. There is no difference between the monitor class and lock, which is hard to understand. For the use of the lock keyword, there is often a very depressing thing "deadlock". When persona has resourcea, resourceb is required to complete the task, when resourceb has resourceb and resourcea is required to complete the task, a deadlock occurs, and the lock keyword does not have any timeout mechanism.

For different monitor classes, the monitor class has a tryenter method. You can specify the timeout value in the parameter list.

 

Code

Private   String Lockflag =   " Lock " ;

Public VoidGowc (person P)
{
Monitor. tryenter (lockflag,100);

Try
{
Console. writeline ("Begin");

.....

Console. writeline ("End");
}
Finally
{
Monitor. Exit (lockflag );
}

 

Sometimes, when a thread enters a synchronization region, it is waiting for another thread to obtain resources. In this case, we can use the wait () Operation of the monitor class to release the lock first, so that other threads can enter and re-enter the synchronization area when resources are available:

Private   Static   Object Ball =   New   Object ();

Static VoidMain (String[] ARGs)
{
Thread threadping= NewThread (NewThreadstart (threadpingproc ));

Thread threadpong= NewThread (NewThreadstart (threadpongproc ));

Threadping. Start ();

Threadpong. Start ();

Threadping. Join ();

Threadpong. Join ();

Console. Readline ();
}

Public Static VoidThreadpingproc ()
{
Console. writeline ("Threadpong: Hello!");

Lock (Ball)
{
For ( Int I =   0 ; I <   5 ; I ++ )
{
Console. writeline ( " Threadpong: Pong " );

Monitor. Pulse (ball );

monitor. Wait (ball);
}< BR >}

Public Static VoidThreadpongproc ()
{
Console. writeline ("Threadping: Hello!");

Lock (Ball)
{
For ( Int I =   0 ; I <   5 ; I ++ )
{
Console. writeline ( " Threadping: Ping " );

Monitor. Pulse (ball );

Monitor. Wait (ball );
}
}
}
}

 

 

(Here we use the p108 code in "C # And. net2.0 practice (Chinese version)

The result is as follows:

 

threadping: Hello !
threadping: Ping
threadpong: Hello !
threadpong: Pong
threadping: Ping
threadpong: Pong
threadping: ping
threadpong: Pong
threadping: Ping
threadpong: Pong

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.