#include"stdafx.h"#include<windows.h>#include<iostream>#include<queue>#include<process.h>using namespacestd; DWORD WINAPI Consumer (void*);//declaring consumer functionsDWORD WINAPI Producer (void*);//declaring producer functions#defineN 10//define the number of buffers/*definition of data structure*/ structmydata{HANDLE M_s_empty;//producer SemaphoreHANDLE M_s_full;//Consumer SemaphoreHANDLE M_m_mutex;//Mutex Signal Volumequeue<int> food;//defining a shared bufferBOOLproducerfinished;//Indicates whether the producer has finished production};intj=0;//just to make the output easier to see thread execution timesintMain () {/*assigning values to each semaphore*/MyData MyData;//Create an entity of the MyData data typeMyData Mydata.m_m_mutex = CreateMutex (NULL,false, NULL);//"false" means that the semaphore you just created is notbelongs to ®¨² any thread mydata.m_s_empty= CreateSemaphore (null, n, n, null);//Initial count is nMydata.m_s_full = CreateSemaphore (NULL,0, N, NULL);//Initial count is 0mydata.producerfinished=false;//The producer end flag is initially set to false to indicate that there is no end/*create producer and consumer threads*/HANDLE handles[2]; handles[0] = CreateThread (NULL,0, &producer, (void*) &mydata,0,0); handles[1] = CreateThread (NULL,0, &consumer, (void*) &mydata,0,0); WaitForMultipleObjects (2, handles,true, INFINITE);//wait for two threads to end before execution goes downCloseHandle (Mydata.m_m_mutex); CloseHandle (Mydata.m_s_full); CloseHandle (Mydata.m_s_empty); }/*producer Functions*/DWORD WINAPI Producer (void*LP) {MyData* MD = (mydata*) LP; for(inti =0I < -; i++) {WaitForSingleObject (MD->m_s_empty, INFINITE);//There's room in the buffer to go down .WaitForSingleObject (Md->m_m_mutex, INFINITE);//consumers do not operate buffer producers before they can execute /*Place the produced item in the specified buffer*/MD->food.push (1); printf ("%d\t produces 1 items, total%d items \t\t%d\n", J++,md->food.size (), GetCurrentThreadID ());//loseout buffer information and thread information ReleaseMutex (MD->m_m_mutex);//release the mutex signal amountReleaseSemaphore (Md->m_s_full,1, NULL);//there is an item buffer to add a} MD->producerfinished=true;//if A For loop is produced, the producer ends the production and controls the end of the consumer thread return 0; } /*Consumer Functions*/DWORD WINAPI Consumer (void*LP) {MyData* MD = (mydata*) LP; while(!md->producerfinished) {//If the producer does not end the production, it can continue to executeWaitForSingleObject (MD->m_s_full,infinite);//if the buffer is not empty, it can be executed downWaitForSingleObject (MD->m_m_mutex,infinite);//If the producer is not operating the buffer, then consumption can be manipulated/*consume an item*/MD-Food.pop (); printf ("%d\t consumes 1 items, total%d items \t\t%d\n", J++,md->food.size (), GetCurrentThreadID ());//output buffer information and thread information ReleaseMutex (MD->m_m_mutex);//release the mutex signal amountReleaseSemaphore (Md->m_s_empty,1, NULL);//an empty buffer adds a} return 0; }
Say the style of