Linux system Programming: Thread synchronization-condition variables (cond)

Source: Internet
Author: User

Thread synchronization-condition variables (cond)

producer and consumer issues

Before introducing conditional variables, let's look at producer and consumer issues: producers are constantly producing products, and consumers are constantly consuming products.

This problem is synchronized in two places: first, consumers need to synchronize: the same product can only be consumed by one person. Second, when no products can be consumed, consumers need to wait for producers to continue to consume, which is another problem of synchronization. Learn more: Producer consumer issues.


condition Variable

Conditional variables are a mechanism for synchronizing with global variables shared between threads, and condition variables are always combined with mutexes.


related Functions

pthread_cond_t   //condition Variable type pthread_cond_initpthread_cond_destroypthread_cond_wait (pthread_cond_t *, Pthread_ mutex_t *) Pthread_cond_timedwaitpthread_cond_signalpthread_cond_broadcast
The pthread_cond_wait () method, which needs to be focused on, has three functions:

    1. All threads running here are blocked until the condition variable wakes up.
    2. Release the lock while blocking.
    3. When awakened, go back to acquiring the lock.
There are two functions that wake up a thread: Pthread_cond_signal and Pthread_cond_broadcast, the former waking one, and the latter waking all.
Producer and Consumer sample codeusing the condition variables, we can solve the problem of producer and consumer:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>struct goods{ int id;struct goods *next;}; pthread_mutex_t m;pthread_cond_t has_product;struct goods *head;void *producer (void *argv) {struct Goods *p = NULL;while ( 1) {Pthread_mutex_lock (&m);p = malloc (sizeof (struct goods));p->id = rand ()% 100;p->next = Head;head = p;printf ( "Produce%d\n", P->id);p thread_mutex_unlock (&m);p thread_cond_signal (&has_product);//printf ("Produce%d \ n ", p->id); Sleep (rand ()% 2);} return (void *) 0; void *comsumer (void *argv) {struct Goods *p = null;while (1) {pthread_mutex_lock (&m);//thinking: The Role of pthread_cond_wait ()? while (NULL = = head) pthread_cond_wait (&has_product, &m);p = Head;head = head->next;printf ("Comsume%d\n", p-& Gt;id);p Thread_mutex_unlock (&m)//printf ("Comsume%d\n", p->id); free (p); Sleep (rand ()% 2);} return (void *) 0; int main (void) {int i;//initialize condition variable and mutex pthread_mutex_init (&m, NULL);p Thread_cond_init (&has_product, null), head = null;pthread_t pro[2], com[3];for (i = 0; i < 2; i++) pthread_create (&pro[i], NULL, producer, NULL); f or (i = 0; i < 3; i++) pthread_create (&com[i], NULL, comsumer, NULL); for (i = 0; i < 2; i++) Pthread_join (Pro[i], NULL); for (i = 0; i < 3; i++) Pthread_join (Com[i], NULL);//Destroy condition variable and mutex Pthread_mutex_destroy (&m);p Thread_cond_ Destroy (&has_product); return 0;}
in the code, we open two threads as producers and three threads as consumers. The product uses a linked list for storage, and each production and consumption occurs at the head of the linked list.

CCPP Blog Directory 

Copyright NOTICE: This article is for bloggers original articles, reproduced, please indicate the source.

Linux system Programming: Thread synchronization-condition variables (cond)

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.