C++多线程编程第四讲--创建多个线程、数据共享问题分析、案例代码
//(1)创建和等待多个线程
#include<iostream>
#include<thread>
#include<vector>
using namespace std;
void myprint(int num)
{
cout << "this thread id = " << std::this_thread::get_id() << " num = " << num << endl;
}
int main()
{
vector<thread> mythreadVec;
int i;
for (i = 0; i < 10; ++i)
{
mythreadVec.push_back(thread(myprint, i)); //创建并开始执行线程
}
for (auto& th : mythreadVec)
{
th.join();
}
cout << "main thread end..." << endl;
return 0;
}
//(2)数据共享问题分析
//(2.1) 只读的数据
#include<iostream>
#include<thread>
#include<vector>
using namespace std;
vector<int> g_v{ 1,2,3 }; //共享数据
void myprint()
{
for (auto& i : g_v)
{
cout << "this thread id = " << std::this_thread::get_id() << " num = " << i << endl;
}
}
int main()
{
vector<thread> mythreadVec;
int i;
for (i = 0; i < 10; ++i)
{
//创建并开始执行线程,每个线程都在读数据,这并不会造成冲突
mythreadVec.push_back(thread(myprint));
}
for (auto& th : mythreadVec)
{
th.join();
}
cout << "main thread end..." << endl;
return 0;
}
//(2.2) 有读有写:如果没有特殊的处理,这种情况程序会崩溃。
//(2.3) 其他案例
//数据共享
/(3)共享数据的保护案例代码
//两个创建的线程,一个线程收集命令,并将命令数据写道一个队列中。
//另外一个线程从队列中取出玩家发送过来的命令,解析,然后执行玩家需要的动作。
#include<iostream>
#include<thread>
#include<vector>
#include<list>
using namespace std;
class A
{
public:
//把收到的消息,入到一个队列中
void inMsgRecvQueue()
{
int i;
for (i = 0; i < 100000; ++i)
{
cout << "push_back num = "<< i << endl;
msgQueue.push_back(i); //数字i就是玩家的命令。
}
}
//把数据从消息队列中取出
void outMsgRecvQueue()
{
int i;
for (i = 0; i < 100000; ++i)
{
if (!msgQueue.empty())
{
//消息不为空
int command = msgQueue.front();
msgQueue.pop_front(); //移除首元素
cout << "command = "<< command << endl;
}
else
{
cout << "msgQueue is empty" << endl;
}
}
}
private:
list<int> msgQueue;
};
int main()
{
A myobj;
//程序运行时会报错,因为存在myInMsg线程正在向队列中写入数据的时候,myOutMsg线程
//将队列中的数据拿出进行操作的错误。如何解决这种问题,在下一节中演示。
thread myOutMsg(&A::outMsgRecvQueue, std::ref(myobj)); //保证线程中用的同一个对象
thread myInMsg(&A::inMsgRecvQueue, std::ref(myobj));
myOutMsg.join();
myInMsg.join();
cout << "main thread end..." << endl;
return 0;
}
posted on 2021-09-28 08:13 xcxfury001 阅读(81) 评论(0) 收藏 举报
浙公网安备 33010602011771号