Producer-consumer models for thread concurrency:
1. Two processes operate on the same memory resource, one for the producer, and the other for the consumer.
2. The producer fills in the data to the shared memory resource and waits for the consumer to consume the data if the area is full.
3. The consumer takes data from the shared memory resource and waits for the producer to populate the data if the area is empty.
4. The producer's fill data behavior and consumer consumption data behavior cannot occur at the same time.
The following API simulates the producer-consumer model with Windows semaphores and threads
#include <Windows.h>#include<stdio.h>#defineN 100#defineTRUE 1typedefintSemaphore; Semaphore Full=0, Empty = N;//shared resource area full slot number and empty slot numberint inch=0, out=0;//buffer production, consumption data pointersHANDLE Mutex;intproducerthread[5];intconsumerthread[5];intbuffer[n+4];//buffersintProduce_item () {//production (random number) return(rand ()%N + N)%N;}intInsert_item (intItem) {//Insert Resource inch%=N; printf ("production to buffer slot:%d\n",inch); buffer[inch] =item; returnbuffer[inch++];}intRemove_item () {//move out of resources out%=N; printf ("take the number of buffer slots%d \ n", out); returnbuffer[ out++];}intConsume_item (intItem) { //Consume it}voidDown (HANDLE HANDLE) {//wait/pWaitForSingleObject (handle, INFINITE);}voidUp (HANDLE HANDLE) {//signal/vReleaseSemaphore (Handle,1, NULL);} DWORD WINAPI producer (LPVoid v) {intitem; while(TRUE) {Item=Produce_item (); if(Empty >0) {//Down (empty)empty--; Down (mutex); //Down (mutex)Insert_item (item); full++;//Up (full)Up (mutex);//Up (Mutex)} Sleep ( -); } return 1;} DWORD WINAPI Consumer (LPVOID v) {intitem; while(TRUE) {if(Full >0) {//Down (full)full--; Down (mutex); //Down (mutex)item =Remove_item (); Consume_item (item); Empty++;//Up (empty)Up (mutex);//Up (Mutex)} Sleep ( -); } return 1;}intMain () {DWORD Tid; Mutex= CreateSemaphore (//Create mutex semaphore mutexNULL,1, 1, NULL); for(intI=0;i<4; i++) {Producerthread[i]= i+1; CreateThread (//Create a producer threadNull//Unable to quilt thread inheritance 0,//Default stack sizeProducer//producer Functions&producerthread[i],//Pass the reference 0,//execute immediately after creation&tid//Thread ID ); Consumerthread[i]= i+1; CreateThread (NULL,0, Consumer,&consumerthread[i],0, &tid);//Create a consumer thread} Sleep (20000); return 0;}
Operation Result:
Or use a custom semaphore mutex to implement:
#include <Windows.h>#include<stdio.h>#defineN 100#defineTRUE 1typedefintSemaphore; Semaphore Mutex=1;//Mutex Signal VolumeSemaphore full =0, Empty = N;//Number of full slots and empty slots in critical areaint inch=0, out=0;//buffer production, consumption data pointersintproducerthread[5];intconsumerthread[5];intbuffer[n+4];//buffersintProduce_item () {//Production random number return(rand ()%N + N)%N;}intInsert_item (intItem) {//inserting a critical section inch%=N; printf ("production to buffer slot:%d\n",inch); buffer[inch] =item; returnbuffer[inch++];}intRemove_item () {//Remove the critical section out%=N; printf ("take the number of buffer slots%d \ n", out); returnbuffer[ out++];}intConsume_item (intItem) { //Consume it}dword WINAPI producer (LPVoid v) {intitem; while(TRUE) {Item= Produce_item ();//Production Itemsempty--;//P (Empty) if(Empty <0)//no empty slots to add dataempty++;//restore empty, continue to loop wait Else if(Mutex >0) {//Otherwise, if the mutex = 1, the critical section is not accessedmutex--;//LockingInsert_item (item);//fill in the data in the critical areafull++;//number of slots plus 1mutex++;//Release Lock} Sleep ( -); } return 1;} DWORD WINAPI Consumer (LPVOID v) {intitem; while(TRUE) { full--;//P (Full) if(Full <0)//If there is no full slot, cannot consumefull++;//restore full, continue waiting Else if(Mutex >0) {//Otherwise, if the mutex = 1, the critical section is not accessedmutex--;//Lockingitem = Remove_item ();//move data out of the critical sectionConsume_item (item);//Consumerempty++;//number of empty slots plus 1mutex++;//Release Lock} Sleep ( -); } return 1;}intMain () {DWORD Tid; for(intI=0;i<4; i++) {Producerthread[i]= i+1; CreateThread (NULL,0, producer,0,0,&Tid); Consumerthread[i]= i+1; CreateThread (NULL,0, Consumer,0,0,&Tid); } Sleep (20000); return 0;}
can also achieve results:
"Windows" implements producer-consumer models with semaphores