当前位置:网站首页>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;
}
边栏推荐
- 2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
- Notes on writing questions in C language -- monkeys eat peaches
- [competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
- Format analysis and explanation of wav file
- oracle 函数 触发器
- Grabcut image segmentation in opencv
- What are the PMP scores?
- Puzzle (019.2) hexagonal lock
- 22 mathematical modeling contest 22 contest C
- Cassava tree disease recognition based on vgg16 image classification
猜你喜欢

Why should the terminal retail industry choose the member management system
![[IOU] intersection over union](/img/17/a3e2144dfbd9248034cae285ee85bd.jpg)
[IOU] intersection over union

Study on correlation of pumpkin price and design of price prediction model based on BP neural network
![[design completion - opening report] zufeinfo 2018 software engineering major (including FAQ)](/img/b7/fb7ead787e00047f3663e8b5acfe33.jpg)
[design completion - opening report] zufeinfo 2018 software engineering major (including FAQ)

Data-driven anomaly detection and early warning of 21 May Day C

Rxjs TakeUntil 操作符的学习笔记

Vscode attempted to write the procedure to a pipeline that does not exist
![[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most](/img/28/c5c6b6d03b459745dc3735f8b39ea9.jpg)
[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most

Solution to the problem of repeated startup of esp8266

SQL advanced
随机推荐
请问在手机上开户股票,还是去证券公司开户安全?
[smart agriculture program] smart agriculture small program project is currently popular.
Reza RA series - development environment construction
Voiceprint Technology (II): Fundamentals of audio signal processing
neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
Solution to the problem of repeated startup of esp8266
[Ruby on rails full stack course] course directory
Register the jar package as a service to realize automatic startup after startup
Applet cloud development joint table data query and application in cloud function
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
Analysis on the thinking of 2022 meisai C question
Grabcut image segmentation in opencv
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
C语言刷题随记 —— 猴子吃桃
Prediction of pumpkin price based on BP neural network
Format analysis and explanation of wav file
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Neat Syntax Design of an ETL Language (Part 2)
2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded