当前位置:网站首页>Best producer consumer code

Best producer consumer code

2022-06-25 09:51:00 sunny_ yin8899

#include <windows.h>
#include <iostream>
const unsigned short SIZE_OF_BUFFER = 10; // Buffer length
unsigned short ProductID = 0;    // Product number
unsigned short ConsumeID = 0;    // Product number to be consumed
unsigned short in = 0;      // Buffer subscript when the product enters the buffer
unsigned short out = 0;      // The buffer subscript when the product exits the buffer
int g_buffer[SIZE_OF_BUFFER];    // The buffer is a circular queue
bool g_continue = true;      // The control program ends
HANDLE g_hMutex;       // Used for mutual exclusion between threads
HANDLE g_hFullSemaphore;     // When the buffer is full, force the producer to wait
HANDLE g_hEmptySemaphore;     // When the buffer is empty, force the consumer to wait
DWORD WINAPI Producer(LPVOID);    // Producer thread
DWORD WINAPI Consumer(LPVOID);    // Consumer thread
int main()
{
    // Create each mutex
    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);
    // Adjust the following values , You can find , When there are more producers than consumers ,
    // The production speed is fast , Producers often wait for consumers ; conversely , Consumers often wait
    const unsigned short PRODUCERS_COUNT = 3;  // Number of producers
    const unsigned short CONSUMERS_COUNT = 1;  // Number of consumers
    // Total number of threads
    const unsigned short THREADS_COUNT = PRODUCERS_COUNT+CONSUMERS_COUNT;
    HANDLE hThreads[PRODUCERS_COUNT]; // Of each thread handle
    DWORD producerID[CONSUMERS_COUNT]; // Identifier of the producer thread
    DWORD consumerID[THREADS_COUNT]; // Identifier of the consumer thread
    // Create a producer thread
    for (int i=0;i<PRODUCERS_COUNT;++i){
        hThreads[i]=CreateThread(NULL,0,Producer,NULL,0,&producerID[i]);
        if (hThreads[i]==NULL) return -1;
    }
    // Create consumer thread
    for (i=0;i<CONSUMERS_COUNT;++i){
        hThreads[PRODUCERS_COUNT+i]=CreateThread(NULL,0,Consumer,NULL,0,&consumerID[i]);
        if (hThreads[i]==NULL) return -1;
    }
    while(g_continue){
        if(getchar()){ // Press enter to terminate the program
            g_continue = false;
        }
    }
    return 0;
}
// Make a product . A simple simulation , Output only new products ID Number
void Produce()
{
    std::cerr << "Producing " << ++ProductID << " ... ";
    std::cerr << "Succeed" << std::endl;
}
// Put the newly produced product into the buffer
void Append()
{
    std::cerr << "Appending a product ... ";
    g_buffer[in] = ProductID;
    in = (in+1)%SIZE_OF_BUFFER;
    std::cerr << "Succeed" << std::endl;
    // The current state of the output buffer
    for (int i=0;i<SIZE_OF_BUFFER;++i){
        std::cout << i <<": " << g_buffer[i];
        if (i==in) std::cout << " <-- production ";
        if (i==out) std::cout << " <-- consumption ";
        std::cout << std::endl;
    }
}
// Take a product out of the buffer
void Take()
{
    std::cerr << "Taking a product ... ";
    ConsumeID = g_buffer[out];
    out = (out+1)%SIZE_OF_BUFFER;
    std::cerr << "Succeed" << std::endl;
    // The current state of the output buffer
    for (int i=0;i<SIZE_OF_BUFFER;++i){
        std::cout << i <<": " << g_buffer[i];
        if (i==in) std::cout << " <-- production ";
        if (i==out) std::cout << " <-- consumption ";
        std::cout << std::endl;
    }
}
// A product consumes
void Consume()
{
    std::cerr << "Consuming " << ConsumeID << " ... ";
    std::cerr << "Succeed" << std::endl;
}
// producer
DWORD  WINAPI Producer(LPVOID lpPara)
{
    while(g_continue){
        WaitForSingleObject(g_hFullSemaphore,INFINITE);
        WaitForSingleObject(g_hMutex,INFINITE);
        Produce();
        Append();
        Sleep(1500);
        ReleaseMutex(g_hMutex);
        ReleaseSemaphore(g_hEmptySemaphore,1,NULL);
    }
    return 0;
}
// consumer
DWORD  WINAPI Consumer(LPVOID lpPara)
{
    while(g_continue){
        WaitForSingleObject(g_hEmptySemaphore,INFINITE);
        WaitForSingleObject(g_hMutex,INFINITE);
        Take();
        Consume();
        Sleep(1500);
        ReleaseMutex(g_hMutex);
        ReleaseSemaphore(g_hFullSemaphore,1,NULL);
    }
    return 0;
}
原网站

版权声明
本文为[sunny_ yin8899]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200548398149.html