[C/C ++ multi-thread programming] pthread condition variable

Source: Internet
Author: User
Tags pthread mutex

Conditional variables for multithreaded programming
PthreadPOSIX threads is short for POSIXThread Standard.Mutex mechanism, including mutex (6 of C/C ++ multi-thread programming), pthread mutex, and pthread semaphores (7 of C/C ++ multi-thread programming, mutex can effectively coordinate access to shared resources and is an essential mechanism for multi-thread synchronization. The mutex mechanism also has its drawbacks. When a thread is waiting for a shared resource to meet a certain condition, it must constantly lock and unlock the shared resource to check whether the shared resource meets the condition, this will bring about huge consumption.In this case, a new mechanism is required to solve this problem.
1. Conditional Variable mechanism:
The Conditional Variable mechanism makes up for the defects of the mutex mechanism and allows one thread to send signals to another thread (this means that when some conditions of shared resources are met, A thread can send a signal to notify the waiting thread) to block the waiting thread (when the thread waits for a condition of shared resources, the thread can be blocked, wait for other threads to send signal notifications ).The Conditional Variable mechanism is very efficient in dealing with the problem that waiting for shared resources to meet a certain condition, and has advantages over the mutex mechanism in space consumption.2. Condition variables and mutex:
Conditional Variable mechanism. All threads waiting for a conditional variable form a queue, which is obviously a global shared queue. When a thread enters the waiting state, the mutex must be used to add the thread to the queue to prevent multiple threads from using pthread_cond_wait at the same time. Lock the mutex before calling pthread_cond_wait and unlock the mutex before blocking. This also explains that the pthread_cond_wait function parameter requires mutex.
3. Basic Conditional Variable Functions: # include <semaphore. h>Initialization condition variable:Intpthread_cond_init (pthread_cond_t * cond, pthread_condattr_t * cond_attr );The first parameter of the function is the condition variable pointer, and the second parameter is the condition variable attribute pointer (generally set to NULL ). This function initializes the conditional variable process according to the conditional variable attribute.Unconditional waiting:Intpthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex );The first parameter of the function is the conditional variable pointer, and the second parameter is the mutex pointer. Before this function is called, the thread needs to lock the mutex. The function completes the thread joining the waiting queue operation within the lock state, and the thread enters the waiting state to unlock the mutex. Obtain the mutex again and lock it before the condition is met and leave the pthread_cond_wait function. Therefore, you need to unlock the mutex again after this thread.Notify a thread:Intpthread_cond_signal (pthread_cond_t * cond );The parameter of this function is a conditional variable pointer. This function sends a signal to the first waiting thread in the queue to cancel the blocking status of this thread.Notify all threads:Intpthread_cond_broadcast (pthread_cond_t * cond );The parameter of this function is a conditional variable pointer. This function is used to remove the blocking status of all waiting threads in the queue.Destruction condition variable:Intpthread_cond_destroy (pthread_cond_t * cond );This function destroys the condition variable.
4. Test the knife:
For shared resource I, thread 1 infinitely adds 1 to I and outputs all I values that are not multiples of 5. When the I value is a multiple of 5, the conditional variable mechanism is used to notify thread 2 and thread 2 to output the I value at this time.


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.