Summary of synchronization among Linux threads

Source: Internet
Author: User

Time to do a summary of the synchronization between all threads of Linux (native), including the following:

1, Mutex

2, Condition variable

3, Reader-writer Lock

4, Spin lock

5, barrier

Mutexes are the most common way of synchronizing between threads, and the main purpose is to protect shared resources from being accessed atomically.

Personal feeling condition variable is the second most common thread synchronization method in addition to mutexes, which can be used to synchronously use a thread to notify another thread that an event has occurred. Can be understood as a signal between threads.

Reader-write Lock is a special kind of lock, it has two kinds of lock way, one is to read the way to lock, the other is to write the way to lock. such as it can be held concurrently by multiple read threads, but can only be written by a write thread held separately. If a read-write lock has been obtained by a thread, the subsequent read locking can still acquire the lock, but the lock cannot be acquired by the write lock. If a read-write lock has been acquired by a thread in writing, any other acquisition lock operation will be blocked. In order to prevent the write thread from "starving", all subsequent fetch read locks are blocked when threads are getting a write lock, and this feature is not a mandatory requirement.

Spin lock does not go to sleep when it cannot acquire a lock, that is, it does not release the CPU resources, but is busy waiting until it can acquire the lock. This is not very useful in non-real-time systems, because even if the CPU is not primarily released, it will be re-dispatched by the operating system after the time slice is exhausted. So spin lock should be used only in a very high priority, and only a little bit of waiting to get to the lock.

Barrier is a special kind of synchronization that ensures that multiple threads run to the same place before they are executed down. It can be understood that multiple threads are running on multiple parallel runways, where there is a wall, the power of one thread is not able to tear down the wall, it takes multiple threads to the foot of the wall, and then the resultant force can be pushed down. Then they continue to run together.

Each synchronization mode has done a demo program, see GitHub code HTTPS://GITHUB.COM/CLPSZ/LINUX-ITSS, Welcome to discuss.

Summary of synchronization among Linux threads

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.