Summary of synchronization and mutual exclusion among Linux threads


When threads execute concurrently, we need to ensure the safe access of critical resources to prevent threads from competing for resources and causing data ambiguity.

Thread synchronization: condition variables

Why use conditional variables?

Timing controllability of critical resourcesIf the condition is satisfied, other threads waiting to operate critical resources will be notified, similar to the signal. Scene: T-day exhibition queuing visit / producer consumer model

What are the conditional variables?

It’s a synchronization mechanism,A thread is used to modify this variable to meet the conditions for other threads to continue to execute. Other threads receive the signal that the conditions have changed.

Conditional variable operation?

Initialization and destruction


If the condition is not satisfied, the lock will be released and the waiting will be blocked. This function is an atomic operation: 1. Put the thread into the conditional waiting queue; 2. Release the lock

If the condition is satisfied, the thread will be awakened and locked

pthread_ cond_ Signal one on one wake up

Wake up a thread in the waiting queue

pthread_ cond_ Broadcast wake up

Wakes all threads in the waiting queue

Why atomic operations are required for waiting and unlocking / why mutex is used for condition variables?

Because pthread_ cond_ The purpose of the lock in wait is to protect condition variables and prevent missing signals. If waiting for unlocking is not an atomic operation, for example, thread a unlocks first, then the CPU time slice switches to thread B, thread B locks and sends the condition variable signal, and then switches to thread A. thread a will miss the signal before waiting, and it may block permanently. Therefore, waiting and unlocking must be atomic operations.

Why do you need a while loop to determine whether a critical resource exists?

In the case of one to many, the producer sends a signal that the waiting thread is awakened and locked. However, if only one thread can lock, other threads will block the waiting lock. If this thread runs out of critical resources, it is unreasonable for other threads to continue to go down without judgment.

Does singal want to unlock first or later?

If you unlock first, the lock is obtained by the thread that does not block and wait, and then the critical resource is used, the singal after unlocking is meaningless, that is, false wake-up;

In Linux, there are two queues, one is cond_ Wait, one is mutex_ Lock, singal just let cond_ Transfer threads on wait to mutex_ Lock, will not return to the user space, which can improve efficiency.

Thread mutex: mutex lock

Why use mutex?

Only access critical resources at the same time to protect critical resources from modification. Scene: scalpers snatch tickets

What is a mutex?

It is a 0 / 1 counter. 1 means that there are resources to operate and 0 means that there are no resources to operate.

Mutex operation?

Initialization and destruction

Lock: if the count is 1, set it to 0 to perform the required operation; if the count is 0, the block wait count becomes 1

Unlock — set the count to 1

The above is all the relevant knowledge points of this introduction. Thank you for your learning and support for developer.