ubuntu14.04下编译命令:
g++ -std=c++11 test_list.cpp -o test -pthread
#pragma GCC diagnostic error "-std=c++11"
#include <stdio.h>
#include <list>
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>
#include <mutex>
using namespace std;
mutex mtx;
typedef struct send
{
int command;
list<string> namelist;
}sendmsg;
list<sendmsg> sendmsglist;
void sendthread()
{
sendmsg msg;
for(int i=0;i<10;i++)
{
msg.command=i;
msg.namelist.clear();
msg.namelist.push_back("1111111111");
msg.namelist.push_back("22222222222222");
msg.namelist.push_back("3333333333333333");
mtx.lock();
sendmsglist.push_back(msg);
mtx.unlock();
cout<<"sendthread----"<<i<<endl;
usleep(500000);
}
}
void recvthread()
{
list<sendmsg>::iterator it;
list<string>::iterator it1;
int command=0;
while(1)
{
//cout<<"---------------------000----------------------------------------"<<endl;
mtx.lock();
if(sendmsglist.size()>0)
{
for(it=sendmsglist.begin();it!=sendmsglist.end();it++)
{
cout<<"command:"<<it->command<<endl;
for(it1=it->namelist.begin();it1!=it->namelist.end();it1++)
{
cout<<*it1<<endl;
}
it->namelist.clear();
cout<<"------------------------------------------------"<<endl;
}
sendmsglist.clear();
}
mtx.unlock();
//cout<<"---------------------111----------------------------------------"<<endl;
usleep(1000000);
}
}
int main()
{
thread t1(sendthread);
thread t2(recvthread);
t1.join();
t2.join();
return 0;
}