#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <sstream>
#include <thread>
#include <queue>
#include <mutex>
#include <ctime>
using namespace std;
vector<int> nums1;
vector<int> nums2;
vector<int> new_nums;
string str="gazbvvbzagba";
template <class T>
class m_queue {
private:
queue<T> q;
mutex mut;
public:
m_queue(){}
~m_queue(){}
void insert(T data)
{
lock_guard<mutex> lk(mut);
q.push(move(data));
cout<<"insert data:"<<data<<endl; //数据类型为指针时cout<<"insert data:"<<*data<<endl;
this_thread::sleep_for(chrono::milliseconds(500));
}
T process_data()
{
if(q.empty())
{
return 0;
}
lock_guard<mutex> lk(mut);
T t=q.front();
cout<<"Pop data:"<<t<<endl; //*t
q.pop();
this_thread::sleep_for(chrono::milliseconds(500));
return t;
}
};
m_queue<int> q2; //数据类型为指针类型的情况 m_queue<int*> q2;
void fun1()
{
while(true)
{
for(int i=0;i<100;i++)
{
/*数据类型为指针时:
int *p=&i;
q2.insert(p);
*/
q2.insert(i);
this_thread::sleep_for(chrono::milliseconds(1));
}
}
}
void fun2()
{
while(true)
{
for(int i=0;i<100;i++)
{
q2.process_data();
this_thread::sleep_for(chrono::milliseconds(1));
}
}
}
int main()
{
m_queue<int> q1;
thread t1(fun1);
thread t2(fun2);
t1.join();
t2.join();
return 0;
}