Linux Component Package Two: Condition

Source: Internet
Author: User
Tags mutex

This post discusses the encapsulation of conditional variable condition in Linux.

The condition variable condition mainly describes the synchronization between threads, that is, the collaborative relationship .

The condition variables in Linux typically involve the following functions:

int pthread_cond_init (pthread_cond_t *cond, pthread_condattr_t *cond_attr); int pthread_cond_signal (pthread_cond_t *cond); int pthread_cond_broadcast (pthread_cond_t *cond); int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); int Pthread_cond_destroy (pthread_cond_t *cond);

First, the Condition variable class (Condition):

1#ifndef Condition_h_2#define Condition_h_34 #include"NonCopyable.h"5 #include"MutexLock.h"6 #include <pthread.h>78Class Condition:noncopyable//Private inheritance9{10Public:Condition (Mutexlock &Mutex);12 ~Condition ();1314void Wait ();//Wait15void signal ();//Send Signal16Private:17pthread_cond_t Cond_;Mutexlock &mutex_;//Parameters for the wait function19};20Condition::condition (Mutexlock &Mutex22: Mutex_ (Mutex)23{Pthread_cond_init (&Cond_, NULL);25}26condition::~Condition ()28{Pthread_cond_destroy (&Cond_);30}3132voidCondition::wait ()33 {34 pthread_mutex_t lock = Mutex_.getmutex (); 35 pthread_cond_wait (&cond_, &lock); 36 }37 38 void Condition::signal () 39 {40 Pthread_cond_ Signal (&cond_); }42  #endif   

here are a few things to note:

1),Wait must be under the conditions of the lock can be used;

2), thesignal function wakes one thread at a time and is usually used to notify the resource to be available;

3),Broadcast function one time to notify multiple threads, to notify the state of change , misuse of it, will lead to "surprise group" problem;

When you use the wait function, you must use the while loop . The reasons are as follows :

1, the use if statement, at most can only judge once;

2, in order to describe the convenience, we assume that there are 5 producers (producer), 3 consumers (consumer), buffer in the maximum can be filled with 10 products;

A) Assuming that there are already 10 products in the buffer, and if you use producer A to grab the lock, you call the wait function, block the producer function, send a signal to the consumer, and release the lock.

b) At this time there is a consumer grab the lock, will naturally consume a product, and release the lock, when the number of products there are 9;

c) Since the grab lock is not queued , it is possible that except a producer (assuming B) grab the lock, because the buffer is not full (9), so the production of a product, the buffer is full;

D) or because the lock is not queued, then it is possible that a and regain the lock, because if will only be judged once , and has been nearly executed, therefore, a producer will produce a product, because the buffer is full,

At this point, if the production of products, it will cause overflow.

e) If the whilt loop is used , A if the lock is obtained, the state of the buffer (whether full) will be determined first, and if it is full, it will enter the blocking state without causing overflow.

3, in order to prevent broadcast interference: If there is only one resource, using broadcast will wake up all the waiting threads, then multiple threads are awakened, but eventually only one can get the resources, this is called "Surprise group Effect".

Linux Component Package Two: Condition

Related Article

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.