In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
When threads execute concurrently, we need to ensure safe access to critical resources to prevent threads from competing for resources, resulting in data ambiguity.
Thread synchronization: condition variabl
Why use conditional variables?
For the timing controllability of the critical resource, the condition will notify other threads waiting for the operation of the critical resource, similar to the signal. Scene: T-DAY exhibition queue visit / producer-consumer model
What is the condition variable?
Is a synchronization mechanism in which one thread modifies this variable to meet the conditions under which other threads continue to execute, and other threads receive signals that the conditions have changed.
Conditional variable operation?
Initialize and destroy
Pthread_cond_wait
If the condition is not met, the lock will be released and the wait will be blocked. This function is atomic: 1. Put the thread in the conditional waiting queue 2. Release lock
If the condition is met, the thread will be awakened and locked.
Pthread_cond_signal one-on-one awakening
Wake up a thread in the waiting queue
Pthread_cond_broadcast broadcast Wake up
Wake up all threads in the waiting queue
Why does waiting and unlocking require atomic operations / why do conditional variables use mutexes?
Because the lock in pthread_cond_wait is to protect condition variables from missing signals, if waiting for unlocking is not an atomic operation, such as thread A unlocking first, then CPU time slice switches to thread B, thread B locks and sends condition variable signals, and then switches to thread A, thread A misses the signal before waiting, and may permanently block. Therefore, waiting and unlocking must be atomic.
Why do you need a while cycle to determine whether a critical resource exists?
In an one-to-many case, the producer sends a signal that the waiting thread is awakened and locked, but only one thread can lock it, and the other threads block the waiting lock if the thread runs out of critical resources. it is unreasonable for other threads to go down without judgment.
Should singnal be unlocked first or later?
If the lock is unlocked first, the lock is acquired by the thread that is not blocking waiting, and then the critical resource is used, the unlocked singal is meaningless, that is, false awakening.
First singal wakes up, and then let the awakened threads scramble for locks. Under linux, there are two queues, one is cond_wait, and the other is mutex_lock,singal, which only transfers threads on cond_wait to mutex_lock and does not return user space, which improves efficiency.
Thread mutex: mutex
Why use mutexes?
Unique access to critical resources at the same time to protect critical resources from modification. Scene: scalpers grab tickets
What is a mutex?
It is a counter of 0 to 1, where 1 means there are resources to operate, and 0 means there are no resources to operate on.
Mutex operation?
Initialize and destroy
Lock-if the count is 1, set 0 to perform the desired operation; if the count is 0, the blocking wait count becomes 1
Unlock-count set to 1
These are all the relevant knowledge points of this introduction. Thank you for your study and support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.