#include <iostream>
#include <thread>
using namespace std;
const int buffer_size = 100;
int in = 0, out = 0;
int cur = 0;
int buffer[buffer_size];
void Producer()
{
while((in + 1) % buffer_size == out);
buffer[(in+1)%buffer_size] = ++cur;
in = (in + 1) % buffer_size;
cout << "第" << cur << "的数据被放入" << endl;
}
void Consumer()
{
while(out == in);
cout << "第" << buffer[(out + 1)%buffer_size ] << "的数据被拿出" << endl;
out = (out + 1) % buffer_size;
}
int main()
{
int T = 100;
while(T--) {
thread producer(Producer);
thread consumer(Consumer);
producer.detach();
consumer.detach();
}
return 0;
}
消费者生产者阻塞模式C++简略代码
最新推荐文章于 2022-06-06 17:26:06 发布