1#include <windows.h>2#include <iostream>3 ConstUnsigned ShortSize_of_buffer =Ten;//buffer length4Unsigned ShortProductID =0;//Product number5Unsigned ShortConsumeid =0;//The product number that will be consumed6Unsigned Short inch=0;//buffer subscript When the product enters the buffer7Unsigned Short out=0;//buffer subscript When product is out of buffer8 intG_buffer[size_of_buffer];//buffer is a cyclic queue9 BOOLG_continue =true;//Control Program EndTenHANDLE G_hmutex;//used for mutual exclusion between threads OneHANDLE G_hfullsemaphore;//forcing producers to wait when the buffer is full AHANDLE G_hemptysemaphore;//forcing the consumer to wait when the buffer is empty -DWORD WINAPI Producer (LPVOID);//Producer Threads -DWORD WINAPI Consumer (LPVOID);//Consumer Threads the intMain () - { - inti; - //create individual mutex signals +G_hmutex =CreateMutex (null,false,null); -G_hfullsemaphore = CreateSemaphore (null,size_of_buffer-1, size_of_buffer-1, NULL); +G_hemptysemaphore = CreateSemaphore (NULL,0, size_of_buffer-1, NULL); A //by adjusting the values below, you can see that when the number of producers exceeds the number of consumers, at //production is fast, producers often wait for consumers; Conversely, consumers often wait for - ConstUnsigned ShortProducers_count =3;//Number of producers - ConstUnsigned ShortConsumers_count =1;//Number of consumers - //total number of threads - ConstUnsigned ShortThreads_count = producers_count+Consumers_count; -HANDLE Hthreads[producers_count];//handle of each thread inDWORD Producerid[consumers_count];//identifier of the producer thread -DWORD Consumerid[threads_count];//identifier of the consumer thread to //Create a producer thread + for(i=0; i<producers_count;++i) { -Hthreads[i]=createthread (NULL,0, Producer,null,0,&producerid[i]); the if(Hthreads[i]==null)return-1; * } $ //Create a consumer threadPanax Notoginseng for(i=0; i<consumers_count;++i) { -Hthreads[producers_count+i]=createthread (NULL,0, Consumer,null,0,&consumerid[i]); the if(Hthreads[i]==null)return-1; + } A while(g_continue) { the if(GetChar ()) {//Terminate program run after press ENTER +G_continue =false; - } $ } $ return 0; - } - //produce a product. Simple simulation, only output the new Product ID number the voidProduce () - {WuyiStd::cerr <<"producing"<< ++productid <<" ... "; theStd::cerr <<"succeed"<<Std::endl; - } Wu //put the newly produced product into the buffer zone - voidAppend () About { $Std::cerr <<"appending a product ..."; -g_buffer[inch] =ProductID; - inch= (inch+1)%Size_of_buffer; -Std::cerr <<"succeed"<<Std::endl; A //the current state of the output buffer + for(intI=0; i<size_of_buffer;++i) { theStd::cout << i <<": "<<G_buffer[i]; - if(i==inch) Std::cout <<"<--Production"; $ if(i== out) Std::cout <<"<--Consumption"; theStd::cout <<Std::endl; the } the } the //remove a product from the buffer - voidTake () in { theStd::cerr <<"taking a product ..."; theConsumeid = g_buffer[ out]; About out= ( out+1)%Size_of_buffer; theStd::cerr <<"succeed"<<Std::endl; the //the current state of the output buffer the for(intI=0; i<size_of_buffer;++i) { +Std::cout << i <<": "<<G_buffer[i]; - if(i==inch) Std::cout <<"<--Production"; the if(i== out) Std::cout <<"<--Consumption";BayiStd::cout <<Std::endl; the } the } - //consume a product - voidconsume () the { theStd::cerr <<"Consuming"<< Consumeid <<" ... "; theStd::cerr <<"succeed"<<Std::endl; the } - //producers the DWORD WINAPI Producer (lpvoid Lppara) the { the while(g_continue) {94 WaitForSingleObject (g_hfullsemaphore,infinite); the WaitForSingleObject (g_hmutex,infinite); the produce (); the Append ();98Sleep ( the); About ReleaseMutex (G_hmutex); -ReleaseSemaphore (G_hemptysemaphore,1, NULL);101 }102 return 0;103 }104 //Consumer the DWORD WINAPI Consumer (lpvoid Lppara)106 {107 while(g_continue) {108 WaitForSingleObject (g_hemptysemaphore,infinite);109 WaitForSingleObject (g_hmutex,infinite); the Take ();111 consume (); theSleep ( the);113 ReleaseMutex (G_hmutex); theReleaseSemaphore (G_hfullsemaphore,1, NULL); the } the return 0;117}
Producer Consumer issues (C + + implementation)