In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the difference between conditional variables and semaphores in linux". In daily operation, I believe that many people have doubts about the differences between conditional variables and semaphores in linux. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what are the differences between conditional variables and semaphores in linux?" Next, please follow the editor to study!
Differences: 1, the use of condition variables can wake up all waiters, and semaphore can not wake up; 2, semaphore always has a value (state), and the condition variable is no value, there is no place to record the number of times to send signals, there is no place to record the number of times wait returned; 3, the semaphore is intended to synchronize between processes, condition variables are intended to synchronize between threads.
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Conditional variable
Conditional variable (cond) is a common method used to implement the "wait-> wake-up" logic in multithreaded programs, which is a mechanism for inter-process synchronization. Condition variables are used to block a thread until condition satisfaction is triggered, usually using both condition variables and mutexes.
General condition variables have two states:
(1) one / more threads are suspended waiting for the condition of a conditional variable to be established.
(2) another thread notifies other threads when the conditional variable condition is established.
Use of conditional variables:
# include struct msg {struct msg * masked next; / *. More stuff here... * /}; struct msg * workq;pthread_cond_t qready = PTHREAD_COND_INITIALIZER;pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;voidprocess_msg (void) {struct msg * mp; for (;;) {pthread_mutex_lock (& qlock); while (workq = = NULL) pthread_cond_wait (& qready, & qlock); mp = workq; workq = mp- > m_next Pthread_mutex_unlock (& qlock); / * now process the message mp * /}} voidenqueue_msg (struct msg * mp) {pthread_mutex_lock (& qlock); mp- > m_next = workq; workq = mp; pthread_mutex_unlock (& qlock); pthread_cond_signal (& qready);}
Of course, you can also use the following code when triggering condition variables, both of which have their own advantages and disadvantages
Voidenqueue_msg (struct msg * mp) {pthread_mutex_lock (& qlock); mp- > m_next = workq; workq = mp; pthread_cond_signal (& qready); pthread_mutex_unlock (& qlock);}
Semaphore
Semaphore is a special variable, and the access is atomic.
Only two operations are allowed on it:
(1) wait for semaphore
When the signal value is 0, the program waits; when the signal value is greater than 0, the semaphore is minus 1, and the program continues to run.
(2) send semaphore
Add 1 to the semaphore value.
Description: Linux provides a set of semaphores API, declared in the header file sys/sem.h.
The difference between linux condition variables and semaphores:
(1) the use of conditional variables can wake up all waiters at once, and the function that this semaphore does not have is the biggest difference.
(2) the semaphore always has a value (state), but the condition variable does not exist, and there is no place to record how many times the wake-up (send a signal) or how many times the wake-up thread (wait returns). In terms of implementation, a semaphore can be implemented with mutex + counter + condition variable. Because semaphores have a state, there may be something special about semaphores if you want precise synchronization. Semaphores can solve the problem of wake-up loss in conditional variables.
(3) the intention of semaphore is inter-process synchronization, the intention of mutex and condition variables is inter-thread synchronization, but semaphores can also be used between threads, mutex and condition variables can also be used between processes. A decision should be made on the basis of the actual situation. The most useful scenario for semaphores is to indicate the amount of resources available.
A classic sentence:
Mutex is a special case of semaphore, and the essence of mutex is a lock.
At this point, the study on "what is the difference between conditional variables and semaphores in linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.