Condition variables to achieve thread synchronization

(1) What is the condition variable to achieve thread synchronization?
  If there are two threads in our program, one is a producer thread and the other is a consumer thread. The producer thread writes data to the buffer buffer at intervals. , And the consumer thread takes data from the buffer every once in a while. To avoid confusion between the two threads reading and writing, we let the production thread finish writing and then notify the consumer to read the data, then you can use condition variables to implement the thread Synchronize.

(2) Two actions of condition variable
  condition is not satisfied: blocked thread
  condition is satisfied: notify blocked thread to start work

(3) Types of condition variables:

pthread_cond_t cond;

(4) Main function:
Initialize a condition variable (the second parameter is generally NULL)

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);

Destroy a condition variable

int pthread_cond_destroy(pthread_cond_t *cond);

Blocking waiting for a condition variable

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);

       The blocking thread
       unlocks the locked mutex and unlocks the mutex
       after unlocking

Wait for a condition variable for a limited time

int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);

Activate a thread waiting for this condition. When there are multiple waiting threads, activate one of them in the enqueue order.

int pthread_cond_signal(pthread_cond_t *cond);

Wake up all threads blocked on condition variables

int pthread_cond_broadcast(pthread_cond_t *cond);

 

(5) Precautions

Note 1: The
mutex mutex must be an ordinary lock (PTHREAD_MUTEX_TIMED_NP) or an adaptive lock (PTHREAD_MUTEX_ADAPTIVE_NP).
Before calling pthread_cond_wait (), this thread must lock pthread_mutex_lock ().
The mutex remains locked until the condition waiting queue is updated.
Unlock before the thread hangs into wait.
Note 2:
Before the condition is satisfied and you leave pthread_cond_wait (), the mutex will be re-locked to correspond to
the lock action before entering pthread_cond_wait ().
In other words, before doing pthread_cond_wait, it is often necessary to use pthread_mutex_lock to lock, and the
pthread_cond_wait function will unlock the lock, and then suspend the thread. Until the condition is
triggered by pthread_cond_signal, the function will restore the lock state to the locked state, and
finally use
pthread_mutex_unlock to unlock.

Note 3:
Both pthread_cond_wait () and pthread_cond_timedwait () are implemented as cancellation points, which means that if
pthread_cond_wait () is cancelled, the block is exited, and then the lock state is restored, and then the current thread is terminated. That
mutex lock has returned to the state, but the current thread has been canceled, then the mutex will not be solved, this
time the lock is not released, it will cause a deadlock, thus requiring the thread exits before Unlock it.

 

(5) Example

1.初始化条件变量和互斥锁
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

2.在线程中使用
(1)生产者线程
    pthread_mutex_lock(&mutex);// 使用互斥锁保护共享数据

    对缓冲区buffer写入操作
    
    pthread_cond_signal(&cond);// 通知阻塞的消费者线程,数据写入完毕,可以解除阻塞了
    pthread_mutex_unlock(&mutex);

(2)消费者线程
    pthread_mutex_lock(&mutex);  //调用pthread_cond_wait前一般都会搭配pthread_mutex_lock
    pthread_cond_wait(&cond, &mutex);    //等待生产者线程的通知
    从缓冲区buffer中读出数据
    pthread_mutex_unlock(&mutex);        //解锁

3.销毁条件变量和互斥锁
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

 

Reference articles for this article:

https://www.cnblogs.com/hesper/p/10738996.html

https://www.cnblogs.com/harlanc/p/8596211.html

 

Published 42 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/qq_37659294/article/details/104273874