For the front end of thread synchronization for the first time, always the mutex, condition variables, semaphore and other terms are not clear, here according to their own understanding to make a simple summary, if there is any omissibility, welcome to criticize.

The mutex

In a multithreaded environment there are often caused by one resource is at the same time to access the resources inconsistent problem, the mutex Through exclusive, that is, at the same time only allow a visitor on a visit to the synchronization to ensure that the resources effectively, but it doesn’t limit access to the resource order thread, so the thread is disorderly access to resources.

spinlocks

In A mutex, if thread A requests A lock and finds that the lock is already occupied by thread B, thread A will sleep until the lock is released by thread B and awakened by the system. But in A spin lock, thread A does not go to sleep when it finds that the lock is occupied. Instead, thread A keeps looping to see if the lock holder has released the lock. At first glance, this hogging of CPU resources is extremely inefficient, but a close comparison with mutex shows several advantages:

  • The implementation is simple: only need to detect the status of the lock loop, noThe mutexSleep, wake up involved in a series of context switches, CPU preemption and other complex processes.
  • Efficient: It is extremely efficient in some cases because the implementation is extremely simple.

Of course, the above efficiency is also conditional, because it takes up CPU resources, so it is mainly suitable for the following scenarios:

  • The critical section has short locking time and no CPU resource strain.
  • Mostly used in multi-core environments.

Recursive locking

Recursive locking and reentrant lock, and the main difference is in the same thread mutex, can obtain the lock resources in the other thread must wait for the thread to release the lock to get the number of corresponding, its main purpose is to solve the deadlock problem within the same process, but in a different thread, it has to do with the mutex and no difference.

Read-write lock

The mutex, spin, and recursive locks described above are all exclusive locks, in which one thread acquires a lock resource and the other threads must wait until the lock is released. However, this mechanism is inefficient in some cases where there is too much reading and too little writing, so the read-write lock was created to solve this problem. Read/write locks are divided into read locks and write locks. They have the following features:

  • Read lock: If thread A acquires A read lock, thread B acquires A read lock, but not A write lock.
  • Write lock: If thread A acquires A write lock, thread B can neither acquire A read lock nor acquire A write lock.
  • Write lock priority: If thread A applies for A write lock and thread B applies for A read lock, thread A is assigned the write lock first.

Condition variables,

Condition variables are applicable to scenarios where a thread needs to wait for a shared resource to meet a condition to perform a series of synchronous operations. It mainly contains:

  • The wait thread that waits for a condition to be true.
  • A signal sending thread that meets a condition.

It is generally used with a mutex (mainly used to protect a Shared resource), in the waiting thread, if conditions are not set up, the thread will automatically block, and release to wait for state changes mutex, if the signal sends thread changed conditions, it sends a signal to the associated condition variables, and awaken the waiting thread, waiting for the thread to acquire a mutex, reassess conditions.

A semaphore

Semaphores are used when a thread needs to wait for another thread to complete some operation before continuing to perform its operation. The main difference between semaphores and the above locks is:

  • Locking is to solve the synchronization problem of shared resources, whileA semaphoreThe problem to be solved is task synchronization between threads.
  • Locks must be locked and unlocked in the same process, whileA semaphoreIt can be acquired in one thread and released in another.

After careful reflection, we will find that the problems to be solved by semaphores can also be dealt with by conditional variables. Compared with semaphores, conditional variables have the following disadvantages:

  • Condition variables,You need global shared variables as wellThe mutexTo achieve state detection.
  • Condition variables,Applicable to multi-threaded environment, not applicable to multi-process environment.

barrier

A barrier is a synchronization mechanism that allows one thread to wait until all its partners reach a condition, and then proceed from that condition.

conclusion

This article briefly summarizes the features and applicable scenarios of the terms involved in thread synchronization. If you find any mistakes in the process of reading, please leave a comment and correct them. We will learn and make progress together. ^ _ ^