C # Learning Notes---Thread synchronization: Mutex, semaphore, read-write lock, condition variable

Source: Internet
Author: User

Http://www.cnblogs.com/maxupeng/archive/2011/07/21/2112282.html

One, mutex (mutex)

The mutex is essentially a lock that locks the mutex before accessing the shared resource, releasing the lock on the mutex after the access is complete.

When the mutex is locked, any other thread that attempts to lock the mutex again will be blocked until the current thread releases the mutex. If multiple threads are blocked when a mutex is released, all blocked threads in the mutex become operational, the first thread that becomes running can lock the mutex, and the other threads will see the mutex locked and can only go back and wait for it to become available again. In this case, only one thread can execute forward at a time.

Second, signal volume (semaphore)

Mutexes can only be used for mutually exclusive access to a resource, and it cannot implement multi-threaded mutex issues for multiple resources. The semaphore can realize multi-thread mutual exclusion and synchronization of multiple homogeneous resources. When the semaphore is a single-valued semaphore, it is also possible to complete a mutually exclusive access to a resource.

Third, read-write lock

Read and write locks are similar to mutexes, but read-write locks allow for higher parallelism. The mutex is either locked or unlocked, and only one thread can lock it at a time.

Iv. condition variables (condition) and monitors (monitor)

When used with a mutex, the condition variable allows the thread to wait for a particular condition to occur in a non-competitive manner.

C # Learning Notes---Thread synchronization: Mutex, semaphore, read-write lock, condition variable

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.