只能访问queue
例子:
#include<Windows.h>
#include <iostream>
#include<queue>
#include<string>
#include<thread>
#include<mutex>
using namespace std;
::queue<int> q;
::mutex mutex1;
void fun1()
{
while (true)
{
lock_guard<mutex> guard(mutex1);
q.push(clock());//入队列
std::this_thread::sleep_for(1ms);
}
}
void fun2()
{
while (true)
{
lock_guard<mutex> guard(mutex1);
if (!q.empty())//先判断队列是否有元素
{
cout << q.front() << endl;//获取第一个元素的值
q.pop();//弹出第一个元素
}
std::this_thread::sleep_for(2ms);
}
}
int main()
{
thread th1(fun1);
thread th2(fun2);
th1.detach();
th2.detach();
int c = ::getchar();
}
原文:https://www.cnblogs.com/zzr-stdio/p/14435610.html