当前位置:网站首页>Best producer consumer code
Best producer consumer code
2022-06-25 09:51:00 【sunny_ yin8899】
#include <iostream>
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
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 Consumer(LPVOID); // Consumer thread
{
// 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);
// 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
const unsigned short THREADS_COUNT = PRODUCERS_COUNT+CONSUMERS_COUNT;
DWORD producerID[CONSUMERS_COUNT]; // Identifier of the producer thread
DWORD consumerID[THREADS_COUNT]; // Identifier of the consumer 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;
}
if(getchar()){ // Press enter to terminate the program
g_continue = false;
}
}
}
void Produce()
{
std::cerr << "Producing " << ++ProductID << " ... ";
std::cerr << "Succeed" << std::endl;
}
void Append()
{
std::cerr << "Appending a product ... ";
g_buffer[in] = ProductID;
in = (in+1)%SIZE_OF_BUFFER;
std::cerr << "Succeed" << std::endl;
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;
}
}
void Take()
{
std::cerr << "Taking a product ... ";
ConsumeID = g_buffer[out];
out = (out+1)%SIZE_OF_BUFFER;
std::cerr << "Succeed" << std::endl;
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;
}
}
void Consume()
{
std::cerr << "Consuming " << ConsumeID << " ... ";
std::cerr << "Succeed" << std::endl;
}
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;
}
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;
}
边栏推荐
- 203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?
- CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
- 【mysql学习笔记21】存储引擎
- Is it safe to open a stock account through the account opening QR code of the account manager?
- Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
- How to make a self-made installer and package the program to generate an installer
- The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
- neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
- Voiceprint Technology (IV): Engineering deployment of voiceprint recognition
- Encoding format for x86
猜你喜欢
x86电脑上下载debian的arm64的包
瑞萨RA系列-开发环境搭建
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
manhattan_slam环境配置
Notes on writing questions in C language -- monkeys eat peaches
Study on correlation of pumpkin price and design of price prediction model based on BP neural network
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Rxjs TakeUntil 操作符的学习笔记
Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
How much money have I made by sticking to fixed investment for 3 years?
随机推荐
Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
华泰证券在上面开户安全吗?靠谱吗?
Is it safe to open an account in a mobile phone or a securities company?
Neat Syntax Design of an ETL Language (Part 2)
Is it safe to open a stock account through the account opening QR code of the account manager?
Puzzle (019.2) hexagonal lock
[matlab] image binarization (imbinarize function)
Creating a binary tree (binary linked list) from a generalized table
Encoding format for x86
SQL高级
Chitubox micromake l3+ slicing software configuration correspondence
力扣-104. 二叉树的最大深度
Neat Syntax Design of an ETL Language (Part 2)
Test Development Engineer
vscode试图过程写入管道不存在
oracle 函数 触发器
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
Opencv中的GrabCut图像分割
Why should the terminal retail industry choose the member management system
203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?