Producer Consumer Model
Producer consumer model is a classic model, we all know that in the actual software development, a module is responsible for the production of data, a module responsible for processing data, the production of data modules, image to become a producer, and the processing of data modules, known as consumers.
We know that the pattern also requires a buffer between the producer and the consumer, and as an intermediary, the producer puts the data in the buffer, and the consumer pulls the data out of the buffer.
Here we write an example of a producer consumer model based on conditional variables and mutexes:
The code is as follows:
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <pthread.h> typedef int data_type; typedef int* data_type_p; pthread_cond_t cond; pthread_mutex_t lock; typedef struct node { data_type _data; struct node *next; }node_t,*node_p,**node_pp; Node_p head=null; static node_p buy_node (Data_type data ) { node_p tmp= (node_p) malloc (sizeof (node_t)); &NBSP;&NBSP;IF (TMP) { tmp->_data=data; tmp- >next=NULL; return tmp; } return null; } void init_list (NODE_PP&NBSP;HEADP) { *headp=buy_node (0); } void push_node (node_p list,data_type Data) { node_p tmp=buy_node (data); node_p tail=list; while (Tail->next) { tail=tail->next; } tail->next=tmp; } void delete_node (NODE_P&NBSP;TMP) { &NBSP;&NBSP;&NBSP;IF (TMP) { free (TMP); tmp=null; } } int pop_node (node_p list,data_type_p data_p) { if (null==list->next) { *data_p=-2; return -2; } else { node_p tmp=list->next; list->next= tmp->next; *data_p=tmp->_data; &nBsp; delete_node (TMP); } return 0; } void show_list (node_p list) { node_p start=list->next; while (Start) { printf ("%d ", Start->_data); fflush (stdout); start=start->next; } printf ("\ n "); } void *product (Void *arg) { int i=0; while (1) { pthread_mutex_lock (&lock); printf ("product data:%d\n", i); Push_node (head,i++); pthread_mutex_unlock (&lock ); printf ("product done,wake up Consumer...\n "); pthread_cond_signal (&cond); sleep (3); } } void *consumer (Void *arg) { data_type _ Data; while (1) { pthread_mutex_lock (&lock); while ( -2==pop_node (head,&_data)) { &nbSp; pthread_cond_wait (&cond,&lock); } printf ("consumer data:%d\n", _ Data); pthread_mutex_unlock (&lock); &nbSp; sleep (1); } } int main () { init_list (&head); pthread_cond_init (&cond,NULL); pthread_mutex_init (&lock,null); pthread_t tid1,tid2; pthread_create (&tid1,null,product,null); pthread_create (&tid2,null,consumer,null); pthread_join (tid1 , NULL); pthread_join (Tid2,null); return 0; } from the above code we can see that we use a linked list as a buffer, producer production of data end plug into the list, the consumer from the list header data. The operation results are as follows:
650) this.width=650; "Width=" 378 "height=" 288 "title=" ATJ7%HV) ~z3 (v8i4l) JO) xm.png "style=" width:407px;height:291px; " Src= "Http://s2.51cto.com/wyfs02/M01/7F/5C/wKioL1cblV7zJURQAAA8kghRJjs879.png" alt= " Wkiol1cblv7zjurqaaa8kghrjjs879.png "/>
From the results we can see that producer consumers have a good realization of synchronization and mutual exclusion function, producers produce a data, consumers consume a data.
Producer Consumer Model