#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include <future>
#include <shared_mutex>
const int N = 100;
using namespace std;
shared_mutex m;
class W_R
{public:
void input();
void output();
private:queue<int>Recv;
};
void W_R::input()
{
for (int i = 0; i < N; i++)
{
unique_lock<shared_mutex> lock(m);
cout << "插入一个元素" << i<<endl;
Recv.push(i);
}
}
void W_R::output()
{
for(int i=0;i<2*N;i++) {
if (!Recv.empty())
{
shared_lock<shared_mutex> lock(m);
cout << "线程" << this_thread::get_id() << "读取一个元素" << endl;
cout << Recv.back() << endl;
//Recv.pop();
}
else cout << "队列已空,无法取出元素" << endl;
}
cout << "end" << endl;
}
int main()
{
W_R a;
thread in(&W_R::input,&a);
thread out1(&W_R::output,&a);
thread out2(&W_R::output, &a);
in.join();
out1.join();
out2.join();
return 0;
}