Pthread_cond thread synchronization mechanism for third-party libraries

Source: Internet
Author: User

Pthread_cond condition Variable Pthread_cond, another synchronization mechanism between threads. The common mutex allows only one thread to enter the critical section, which is the thread that holds the lock on the mutex, and cond allows multiple threads to enter the critical section at the same time, which is controlled by it, to wake up one of the waiting threads when certain conditions are set up, or to wake up all the waiting threads.

int pthread_cond_wait (pthread_cond_t* cond, pthread_mutex_t* mutex);
int pthread_cond_timewait (pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* tout)

The mutex that is passed to the pthrad_cond_wait protects the condition, the caller passes the locked mutex to the pthread_cond_wait function, the function puts the calling thread into the list of threads waiting for the condition, and then unlocks the mutex when Pthread_ When the cond_wait returns, the mutex is locked again. The function pthread_cond_timewait is similar to pthread_cond_wait, except that it is more than a time-out limit.
When two function calls are returned successfully, the condition needs to be re-checked, because other threads may have changed the condition.

int pthread_cond_signal (pthread_cond_t* cond);
int Pthread_cond_broadcast (pthread_cond_t* cond);

The pthread_cond_signal function wakes up a thread that waits for the condition, and Pthread_cond_broadcast wakes up all the threads waiting to change the condition.

The following example is a simple use of the cond. Using cond, we can write a thread pool more efficiently.


1#include <pthread.h>
2#include <unistd.h>
3#include <stdio.h>
4pthread_mutex_t Mutex;
5pthread_cond_t cond;
6intval = 0;
7void*thread_zero_run (void*arg) {
8 while(true){
9Pthread_mutex_lock (&mutex);//Lock the Mutex
Ten while(Val <= 2) {//condition. use and for double check
Oneprintf ("Thread_zero_run--val:%d, wait for Wake Up\n", Val);
APthread_cond_wait (&cond, &mutex);//Call pthread_cond_wait
-}
-printf ("Therad_zero_run--val:%d, zero it and unlock\n", Val);
theval = 0;//Do sth
-Pthread_mutex_unlock (&mutex);//Unlock the Mutex
-}
-Pthread_exit ((void*) 0);
+}
-void*thread_add_run (void*arg) {
+ while(true){
APthread_mutex_lock (&mutex);//Lock the Mutex
at++val;//Do sth
-Pthread_mutex_unlock (&mutex);//Unlock the Mutex
-Pthread_cond_signal (&cond);//Wake up a therad whick are waiting for the cond
-printf ("After add val:%d and wake-one zero thread for check\n", Val);
-Sleep (1);
-}
inPthread_exit ((void*) 0);
-}
tointMain () {
+pthread_t T_add, T_zero;
-Pthread_cond_init (&cond, NULL);
theif(Pthread_create (&t_add, NULL, Thread_add_run, NULL)) {
*return0;
$}
Panax Notoginsengif(Pthread_create (&t_zero, NULL, Thread_zero_run, NULL)) {
-return0;
the}
+Pthread_join (T_add, NULL);
APthread_join (T_zero, NULL);
the return0;
+}
-

Pthread_cond thread synchronization mechanism for third-party libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.