Summary of synchronization and mutual exclusion knowledge points between Linux threads

Summary of synchronization and mutual exclusion knowledge points between Linux threads

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

Thread synchronization: condition variables

Why use condition variables?

For the timing controllability of critical resources , when the conditions are met, other threads waiting to operate critical resources will be notified, similar to a signal. Scenario: Queueing for T-DAY exhibition/Producer-consumer model

What are condition variables?

It is a synchronization mechanism. One thread is used to modify this variable to satisfy the conditions for other threads to continue to execute, and other threads receive signals that the conditions have changed.

Conditional variable operations?

Initialization and destruction

pthread_cond_wait

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

If the condition is met, the thread will be awakened and locked.

pthread_cond_signal one-to-one wakeup

Wake up a thread in the waiting queue

pthread_cond_broadcast broadcast wakeup

Wake up all threads in the waiting queue

Why do waiting and unlocking need atomic operations/Why do condition variables use mutexes?

Because the lock in pthread_cond_wait is to protect the condition variable and prevent missing the signal, if waiting for unlocking is not an atomic operation, for example, thread A unlocks first, and 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 misses the signal before it has time to wait, and may be blocked forever. Therefore, waiting and unlocking must be atomic operations.

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

In a one-to-many situation, the producer sends a signal, the waiting thread is awakened and locked, but only one thread can lock, and the other threads will be blocked waiting for the lock. If this thread uses up the critical resources, it is unreasonable for other threads to continue to move forward without making a judgment.

Should singnal be unlocked first or later?

If the lock is unlocked first, and the lock is obtained by a thread that is not blocked and waiting, and then the critical resource is used, the singal after unlocking is meaningless, which is a false wakeup;

First singal wakes up, and then let the awakened thread compete for the lock. Under Linux, there are two queues, one is cond_wait and the other is mutex_lock. singal only transfers the thread on cond_wait to mutex_lock and will not return to user space, which can improve efficiency.

Thread mutual exclusion: mutex lock

Why use mutexes?

Only access critical resources at the same time, protecting critical resources from modification. Scenario: Scalpers grab tickets

What is a mutex?

It is a 0/1 counter, 1 means there are resources available for operation, 0 means there are no resources available for operation.

Mutex lock operation?

Initialization and destruction

Lock---If the count is 1, set it to 0 and perform the required operation; if the count is 0, block and wait for the count to become 1

Unlock --- Set the count to 1

The above are all the relevant knowledge points introduced this time. Thank you for your learning and support for 123WORDPRESS.COM.

<<:  Are you still Select *?

>>:  Vue implementation counter case

Recommend

Implementation of WeChat applet message push in Nodejs

Select or create a subscription message template ...

React introduces antd-mobile+postcss to build mobile terminal

Install antd-mobile Global import npm install ant...

MySQL5.7 single instance self-starting service configuration process

1.MySQL version [root@clq system]# mysql -v Welco...

Analysis and practice of React server-side rendering principle

Most people have heard of the concept of server-s...

Implementation of drawing audio waveform with wavesurfer.js

1. View the renderings Select forward: Select bac...

Research on the problem of flip navigation with tilted mouse

In this article, we will analyze the production of...

How to automatically import Vue components on demand

Table of contents Global Registration Partial Reg...

mysql IS NULL using index case explanation

Introduction The use of is null, is not null, and...

HTML web page creation tutorial Use iframe tags carefully

Using iframes can easily call pages from other we...

Detailed explanation of the use of Vue h function

Table of contents 1. Understanding 2. Use 1. h() ...