C # thread wait handle

Source: Internet
Author: User

Mutex object Mutex
privatenew Mutex();publicMethod(){    m.WaitOne();    //执行操作    m.ReleaseMutex();}
Shared resources Semaphore

Semaphore is a semaphore that allows multiple resources to be divided into types simultaneously. An important and representative example of this book is that there are several computers in the library, and there are a number of individuals using computers. is a good example.
There is a counter that blocks when the semaphore reaches the specified maximum value. Wait for the signal to be released. facilitate coordination of work.
Thinking: Semaphore can be used as a case where a large amount of data is sent, but a limited number of threads are needed to handle it, say 1200 people need to receive a message at the same time, but with 40 threads to send.

The lightweight Semaphoreslim

The role of Semaphoreslim and semaphore is the same. But Sempahoreslim is lightweight, not kernel-level, so consumes less resources, is more efficient, and individuals like lightweight things.

EventWaitHandle waiting for events to occur

Mainly divided into: ManualResetEvent and authoresetevnt of two
The main thing is to design an infinite loop function to wait for something to happen after the condition arrives. Stop and wait until it arrives.
Individuals in the spot trading software, the market sent the use of manualrestevent. Because we can not determine when the market will come, so let the market before the arrival of waiting, the market once received, immediately. Set () to release the signal.

Countdownevent Countdown counter

When all counts are 0, the block is released and the system continues to execute.
This function can be used when more than one thread is required to perform certain operations, as soon as conditions are available. Start the next step, for example, before the financial Analysis system login, we need to download the system variety, historical data, news messages, and so on more information download, the download of this information is not suitable for a thread to execute. We put each operation on one thread, and when these threads are finished, we start to open the main program. Very suitable for countdownevent.

Staged synchronization Barrier

Book cited example is very good, Xiao Wang and Xiao Li meet to visit the bird's Nest, Xiao Wang and Xiao Li each from their own home, will go to the nest door set. Gather later to visit the Bird's Nest, after the tour finished. And then go home respectively.
Xiao Wang and Xiao Li, each person's situation is different, so they need to wait for each other to the bird's Nest, then start to visit, the visit may not be the same, and so the visit, need to assemble. And then go home, each rally point, is a step. Suitable for use with barrier.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # thread wait handle

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.