Two scenarios for thread synchronization

Source: Internet
Author: User

In scenario one, I mainly describe the different ways to solve resource contention, and this article will cover the second scenario of thread synchronization.

Scenario Two: Limited quantity, first come first served

Scenario Description : Similar to scenario one, but this time the number of manger is more than one. If the number of people in need is less than the number of manger, then everything is in harmony. But what happens when the number of people exceeds manger? How many people occupy a pit?

Solution : When all the manger are full, the others must wait outside, and only when someone comes out from inside, the next person can go in.

problem Abstraction : When a resource is allowed to be used by a certain number of threads at the same time, a mechanism is needed to block the extra threads until the resource becomes available again.

thread Synchronization Scenarios : Semaphore, Semaphoreslim

Program Characteristics : Limited supply, except the owner, others unconditionally wait; first come first served (who advanced manger, who first used, no sequencing )

The difference between the programmes

The Chinese name of Semaphore is called semaphore, which is sure to be talked about when learning the operating system. And the signal volume, the most often cited is the producer and consumer examples. As in the previous article, I will not repeat here some of the content of the online more, if you are interested, you can go to find degrees Niang, will be hard or brother Gu.

Before you continue reading, make sure you have an understanding of user-mode constructs, kernel-mode constructs, and mixed-mode constructs, and if not, it is recommended that you read the relevant chapters in scenario one first.

kernel mode (kernal-mode)

Semaphore limits the number of threads allowed to access at one time by passing in the container size in the constructor. As with the Mutex described in scenario one is kernel mode, and all have the same ancestor: WaitHandle. However, unlike a Mutex, ownership acquired by thread one can be released by the thread. The following wording is completely legal.

    Task.Factory.StartNew (() =    {        // similar to consumer         semaphore. WaitOne ();        Thread.Sleep ();    });     Thread.Sleep (+);     = =        {// similar to producer         semaphore. Release ();    });

Pros: The ability to provide quantitative limits that can be used across processes.

    // Process One    New Semaphore (11"myname" out creaded);          // Process two    Semaphore s = semaphore.openexisting ("myname");

Cons: Slower than user mode, mixed mode construction, slightly faster than mutex.

Mixed Mode (hybrid-mode)

Semaphoreslim is a new type introduced in. Net 4.0, and is used in a way similar to Semaphore, which is a lightweight Semaphore. Cross-process use is not allowed.

Advantages: Ability to provide quantitative limits, fast speed, better than Semaphore.

Cons: cannot be used across processes.

Summary

The method described in this article is primarily intended to solve the problem of thread synchronization when resources are limited (more than one). If the resource is unique, the effect of using Semaphore s = new Semaphore is actually consistent with the effect of creating a Mutex. At this time the Semaphore will have a new name called Binary Semaphore (two-dollar semaphore).

When should I use Semaphore and when do I use a Mutex?

Remember the scenes highlighted in this series! Use a Mutex if used for resource contention, or use Semaphore if you are using it for limited use.

This article comes from the C # Basic review: Two scenarios of thread synchronization

Two scenarios for thread synchronization

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.