"C #" "Thread" monitor and lock

Source: Internet
Author: User

The so-called lock is that the locked area can only be accessed by a single thread, and other threads wait outside the lock. 
Monitor lock throughMonitor.Enter (obj) andMonitor.Exit (obj) to lock and unlock.
Lock lock is locked by direct lock (obj).
The monitor lock and lock lock are similar, and the real lock lock is a variant of monitor.
Lock (obj)
{
}
The equivalent is:
Try
{    
Monitor.Enter (obj)
}
catch ()
{}
Finally
{
Monitor.Exit (obj)
}
So lock can do, monitor certainly can do, monitor can do, lock not necessarily can do.
Let's talk about the special and lock features of monitor.
Monitor.Enter (obj);// Start LockMonitor.TryEnter (obj, +);//start the lock, is also different from the lock function, a more time setting, is to wait for how long after the time if not to enter, then cancel this operation. Lock will continue to wait. Monitor.Wait (obj);//  discards the current thread's ownership of the resource, allowing other threads to lock in. Then, when the other thread code  Pulse ( let the original thread into the waiting queue ), and then continue to run from behind Waint ()
monitor.pulse (obj);//restore the thread that discarded the control of the resource, put it back into the wait queue, and continue to run the next time directly from Wait ().
Monitor.pulseall (obj);// Restores all the threads that roost the resource control to re-enter the wait queue
Monitor.Exit (obj);// End Lock
Example:
[MethodImpl (methodimploptions.synchronized)]Private voidFirstthread () {//Monitor.Enter (this);Monitor.Wait ( This); MessageBox.Show ("Firstthread"); //Monitor.Exit (this);        }        Private voidSecondthread () {Monitor.Enter ( This); Monitor.Wait ( This); MessageBox.Show ("Secondthread"); Monitor.pulse ( This); Monitor.Exit ( This); }        Private voidThirdthread () {Monitor.Enter ( This); MessageBox.Show ("Thirdthread"); Monitor.pulse ( This); Monitor.Exit ( This); }

Call:

Thread thread =NewThread (NewThreadStart (firstthread)); thread. Name="Thread1"; Thread thread2=NewThread (NewThreadStart (Secondthread)); hread2. Name="thread2"; Thread thread3=NewThread (NewThreadStart (Thirdthread)); Thread3. Name="thread3";thread. Start (); Thread2. Start (); thread3. Start ();

Operation Result:

First the thread will go into the Firstthread method, and then because wait is gone, thread 1 discards ownership, then thread 2 enters Secondthread, because also wait, so thread 3 enters Thirdthread, then the MessageBox pops up " Tirdthread ", then run pulse, so freed up thread 2, so thread 2 will pop up" Secondthread ", then release Thread 1 Popup" Firstthread ".

Each lock must have enter and exit, the inside parameter is an object type, and is the identity of a lock.

Instead of using enter and exit in this Firstthread method, you use the

[MethodImpl (methodimploptions.synchronized)]
This function is equivalent to adding enter and exit separately at the beginning and end of the method.

"C #" "Thread" monitor and lock

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.