How to use the wait and pulse methods of C # Monitor

Source: Internet
Author: User

"Reprint" http://blog.csdn.net/qqsttt/article/details/24777553

Monitor's wait and pulse methods are more complex in the use of synchronous locks in the thread, but they are somewhat more difficult to understand, but they are also very rich in connotation and

Subtle! Through them you can implement synchronization objects such as Autoresetevent,manualresetevent, while also in the efficiency and memory

The use of a quality improvement!

When I look at the monitor object at MSDN today, we find that the demo of the member method under it is illustrated with a secure synchronization queue, but

The code annotations and the professional terminology of MSDN itself are obscure, and this example is not a good idea, and here I have added comments to the key statements.

At the same time, finally combining the code to analyze how this synchronous queue is working with Monitor's wait and pulse guidance:





Now I'm going to do a process analysis of the whole code:

In this particular context, thread precedence: " waiting Queue", "Ready queue", "owning lock thread" This is the focus, more

Times, the core of the subtle relationship is also derived from this sequence of execution.
MSDN Official Note: The Synchronized object contains several references, including a reference to the thread that currently owns the lock, an argument to the ready queue,

and a reference to the wait queue.

My reminder: The threads of the competing object lock are in the ready queue.

In this case, I consider the Firstthread and Secondthread methods to be a A-B thread (which is perfectly possible because a, B is a two-thread

Callback functions are peer worker threads),

The following are the analysis steps:

/* Scenario 1: Suppose a thread acquires a M_smplqueue synchronization object lock:

* 1, start loop, call Monitor.Wait (m_smplqueue): A thread releases itself to the lock of the synchronization object, exile itself to

Wait for the queue (b threads compete for sync locks at the outset so they are in the ready queue) until they get the lock again, or they block.

So the A thread runs here and pauses.

* 2, this time B directly from the ready queue to obtain the M_smplqueue object lock, Monitor.pulse (m_smplqueue):

, a thread is released to the ready queue, and a is ready to acquire ownership of the lock.

* 3, execution loop, monitor.wait (M_smplqueue, +): B thread Will Self-exile to wait queue and release itself to

Exclusive of the synchronization lock, which is set to a timeout of 1S, and is automatically added to the ready if the B thread does not acquire the lock again within 1S

queue, or pulse signal received during this period.

* 4, B thread due to return within 1S False,lock block quickly end, that is, the withdrawal of M_smplqueue exclusive rights, a by the ready

The queue enters the exclusive, continuation of the M_smplqueue.


* 5, the blocking end of Monitor.Wait (M_smplqueue) stated in 1, returns True, executes the following code:
M_smplqueue.enqueue (counter) adds an element to the queue, executes the Monitor.pulse (M_smplqueue) of the next line,
As the 3rd 1S is not there (I believe there is no such slow CPU on the planet), the B thread receives pulses and adds itself to the
Thread queue, counter count +1,a threads of Lock end, a enters the waiting queue.

* 6, since B obtains exclusive rights again from the Ready queue, monitor.wait (m_smplqueue, 1000) returns true,while into

Inside the ring, the first element pops up and prints out. Call Monitor.pulse (M_smplqueue) to add a thread to the ready queue,

While the while ends, the lock block ends, and B exits the exclusive entry of the object lock into the wait queue.


* 7, a continue to follow this rule cycle to know that all the numbers are printed out ...
*


* Scenario 2:B Thread First acquires the M_smplqueue synchronization object lock:
* 1, enter the lock block, Monitor.pulse (m_smplqueue) execution: Because the current a thread is already in the ready queue
So it's not going to work (so you must ask that again.) Yes, you have to ask, did you find out if the a thread started to be
No, it works! That's how it works! ).

* 2, start while (Monitor.Wait (M_smplqueue, 1000)) in the judgment, technical details or follow the above, B at this time
will automatically exile itself to the waiting queue and block here (perhaps 1S expires and will put it in the ready queue, this role
is mainly to prevent deadlocks, because our ready queue can not be empty ah, this on the above I forgot to say here to add), so a
Obtained the M_smplqueue exclusive right, then returned to the above from a first to obtain the line lock process ....
*

* In short, this operation is intended to allow multiple threads to operate on a queue while maintaining synchronization: cannot be out of the queue on countless grounds, once there
A data is immediately available to the team, and the end result is that there is no element in the queue.

*/

How to use the wait and pulse methods of C # Monitor

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.