Producer Consumer Model 1. Multiple threads operate global variable n, which needs to be made into a critical section (to be locked--a lock or a semaphore)2. Call function pthread_cond_wait (&g_cond,&g_ Mutex) let the thread lock on a condition to wait --the essence of the pthread_cond_wait () function is ①: the thread that gets the lock, the lock is temporarily discarded (unlocked) ②: thread hibernation, waiting for ③: thread waits for notification, wakes up to continue execution (regain Lock) - This pthread_cond_wait () function is an atomic operation - note: The lost lock can be obtained by the production line, can also be obtained by the consuming thread, the consumer thread gets caught in wait, the producer thread obtains the signal, wake-up Consumer 3. pthread_cond_signal () The producer gets the lock, produces n, sends a signal to the consumer after the production is finished (wakes the consumer), the producer leaves the critical area
//Producer Consumer Model#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<pthread.h>//defining a thread lockpthread_mutex_t Mutex =pthread_mutex_initializer;//Defining thread Condition Lockspthread_cond_t cond =pthread_cond_initializer;//Defining global VariablesintG_num =0;//define the number of consumer threads#defineConsume_count 2//define the number of producer Threads#defineProduct_count 4//Consumer Threadsvoid*consume_run (void*Arg) { intTnum = (int) arg; while(1) { //because multithreading operates a global variable, it needs to be made into a critical section, a yokePthread_mutex_lock (&mutex); printf ("consumer%d enters the critical section! \ n", tnum); //because even if the consumer is awakened, it is necessary to re-examine the value of the g_num, since the pthread_cond_wait () function may be acquired by other consumers after the lock is lost//causes multiple consumer threads to execute the pthread_cond_wait () function into the wait,//because multiple consumer threads are awakened by pthread_cond_signal (), There is only one resource that may have been used by other consumers//pthread_cond_wait () may also be awakened by other signals//so you need to re-judge and not use if while(g_num = =0) {printf ("consumer%d Start waiting! \ n", tnum); //on the thread lock, wait for the conditionPthread_cond_wait (&cond, &mutex); printf ("consumer%d is awakened! \ n", tnum); } //it means that there is already a resource and can consumeprintf"consumer%d begins to consume! \ n", tnum); G_num--; //UnlockPthread_mutex_unlock (&mutex); Sleep (3); } returnNULL;}//Producer Threadsvoid*product_run (void*Arg) { intTnum = (int) arg; while(1) { //when the producer is larger than the consumer, it is possible to produce too much goods, a limit on the upper limit of the goods, and if the goods are too large, the producers need to sleep//involves access to global variables and requires lockingprintf"number of existing resources%d!\n", g_num); Pthread_mutex_lock (&mutex); if(g_num > -) {printf ("producer%d products have reached the upper limit and go to sleep! \ n", tnum); //If the production product is larger than the upper limit, sleep is required, but the lock should be released before Sleep. for consumers to spendPthread_mutex_unlock (&mutex); Sleep (3); Continue; } Else{printf ("producer%d Production products! \ n", tnum); //If the production product is less than the upper limit, no producer sleep is required, then the productg_num++; //Send condition Signalprintf"producer%d Send Condition signal! \ n", tnum); Pthread_cond_signal (&cond); Pthread_mutex_unlock (&mutex); } pthread_mutex_unlock (&mutex); Sleep (2); } returnNULL;}intMainintArgChar*Args[]) {pthread_t Thrs[consume_count+product_count]; inti =0; for(i =0; I < consume_count; i++) { if(pthread_create (&thrs[i], NULL, consume_run, (void*) i)! =0) {printf ("pthread_create () failed!\n"); return-1; } } for(i =0; I < product_count; i++) { if(pthread_create (&thrs[i +consume_count], NULL, product_run, (void*) i)! =0) {printf ("pthread_create () failed!\n"); return-1; } } //Join for(i =0; I < Consume_count + product_count; i++) { if(pthread_join (thrs[i], NULL)! =0) {printf ("pthread_join () failed!\n"); break; }} printf ("the process has been launched! \ n"); return 0;}
Linux POSIX thread condition variables