In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to analyze the thread barrier based on linux threads-2.3, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Thread barrier is a way of thread synchronization. After a thread completes an operation, it may need to wait for another thread to complete an action, at which point the thread is currently suspended until the other thread completes an operation, and finally all threads are awakened. The barrier has three main functions.
Intpthread_barrier_wait (pthread_barrier_t * barrier) {pthread_descr self = thread_self (); pthread_descr temp_wake_queue, th; int result = 0
_ _ pthread_lock (& barrier- > _ _ ba_lock, self)
/ * If the required number of threads have achieved rendezvous... * / / the number of times pthread_barrier_wait is called reaches the threshold, _ _ ba_present + 1 = = _ _ ba_required if (barrier- > _ _ ba_present > = barrier- > _ _ ba_required-1) {/ *. Then this last caller shall be the serial thread * / result = PTHREAD_BARRIER_SERIAL_THREAD; / * Copy and clear wait queue and reset barrier. * / / blocked thread queue temp_wake_queue = barrier- > _ _ ba_waiting; / / reset field barrier- > _ ba_waiting = NULL; barrier- > _ _ ba_present = 0;} else {result = 0; / / execute pthread_barrier_wait once, plus a barrier- > _ _ ba_present++ / / insert waiting queue enqueue (& barrier- > _ _ ba_waiting, self);}
_ _ pthread_unlock (& barrier- > _ _ ba_lock); / / the number of calls to pthread_barrier_wait is not enough for if (result = = 0) {/ * Non-serial threads have to suspend * / suspend the current thread suspend (self); / * We don't bother dealing with cancellation because the POSIX spec for barriers doesn't mention that pthread_barrier_wait is a cancellation point. * /} else {/ * Serial thread wakes up all others. * / Wake up other threads while ((th = dequeue (& temp_wake_queue))! = NULL) restart (th);}
Return result;}
Intpthread_barrier_init (pthread_barrier_t * barrier, const pthread_barrierattr_t * attr, unsigned int count) {if (count = = 0) return EINVAL
_ _ pthread_init_lock (& barrier- > _ _ ba_lock); / / number of times pthread_barrier_wait needs to be executed barrier- > _ ba_required = count; / / number of times pthread_barrier_wait has been called barrier- > _ _ ba_present = 0; / / call pthread_barrier_wait blocked thread queue barrier- > _ _ ba_waiting = NULL; return 0;}
Intpthread_barrier_destroy (pthread_barrier_t * barrier) {/ / there are threads waiting for if (barrier- > _ _ ba_waiting! = NULL) return EBUSY; return 0;}
We know from the code that the essence of the barrier is counting. Before a certain number is reached, the current thread is blocked, and when the last thread executes the pthread_barrier_wait function and gets a certain number, all threads are awakened.
On how to analyze the linux threads-2.3-based thread barrier to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.