当前位置:网站首页>17.4创建多个线程、数据共享问题分析与案例代码
17.4创建多个线程、数据共享问题分析与案例代码
2022-06-11 10:59:00 【zzyzxb】
一、创建和等待多个线程
//线程入口函数
void myprint(int inum)
{
cout << "myprint线程开始执行了,线程编号id=" << inum << endl;
printf("myprint线程开始执行了,线程编号id=%d \n", inum);
//...干各种事情
cout << "myprint线程结束执行了,线程编号id=" << inum << endl;
printf("myprint线程结束执行了,线程编号id=%d \n", inum);
return;
}
int main()
{
vector<thread> mythreads;
//创建10个线程,线程入口函数统一使用myprint
for (int i = 0; i < 10; ++i)
{
mythreads.push_back(thread(myprint, i)); //创建10个线程,同时这10个线程已经开始执行
}
for (auto iter = mythreads.begin(); iter != mythreads.end(); iter++)
{
iter->join(); //等待10个线程都返回
}
cout << "main主函数执行结束" << endl; //最后执行这句,整个进程退出
return 0;
}
创建10个线程,线程入口函数统一使用myprint
<1>多个线程执行顺序是乱的,跟操作系统内部对线程的运行调度机制有关;
<2>主线程等待所有的子线程运行结束,最后主线程结束;老师推荐 join 的写法,更容易写成稳定的程序;
<3>我们把 thread 对象放入到容器里管理,看起来像个 thread 数组,这对我们一次创建大量的线程并对大量线程管理很方便。
二、数据共享问题分析
vector<int> g_v = { 1,2,3 }; //共享数据,只读
//线程入口函数
void myprint(int inum)
{
cout << "id = " << this_thread::get_id() << "gv的值 = " << g_v[0] << g_v[1] << g_v[2] << endl;
return;
}
int main()
{
vector<thread> mythreads;
//创建10个线程,线程入口函数统一使用myprint
for (int i = 0; i < 10; ++i)
{
mythreads.push_back(thread(myprint, i)); //创建10个线程,同时这10个线程已经开始执行
}
for (auto iter = mythreads.begin(); iter != mythreads.end(); iter++)
{
iter->join(); //等待10个线程都返回
}
cout << "main主函数执行结束" << endl; //最后执行这句,整个进程退出
return 0;
}
<1>只读数据:是安全稳定的,不需要什么特别的处理,直接读就可以;
<2>有读有写:2个线程写,8个线程读,如果代码没有特别的处理,那程序肯定崩溃;
最简单的不崩溃处理,读的时候不能写,写的时候不能读。2个线程不能同时写,8个线程不能同时读;
写的动作分10小步;由于任务切换,导致各种诡异事情发生(最可能的诡异事情还是崩溃);
<3>其他案例
数据共享 订票问题
三:共享数据的保护实战范例
网络游戏服务器,两个自己创建的线程,一个线程收集玩家命令(用一个数字代表玩家发来的命令),并把命令数据写到一个队列中。
另外一个线程从队列中取出玩家发送来的命令,解析,然后执行玩家需要的动作;
vector 和 list
list:频繁的按顺序插入和删除数据时效率高。
vector:容器随机的插入和删除数据效率高。
准备用成员函数作为线程函数的方法来写线程
class MA
{
public:
//把收到的消息(玩家命令)放入到一个队列的线程
void inMsgRecvQueue()
{
for (int i = 0; i < 100000; ++i)
{
cout << "inMsgRecvQueue()执行,插入一个元素: " << i << endl;
msgRecvQueue.push_back(i); //假设这个数字i就是我收到的命令,直接弄到消息队列中
}
}
//把数据从消息队列中取出的线程
void outMsgRecvQueue()
{
for (int i = 0; i < 100000; ++i)
{
if (!msgRecvQueue.empty())
{
//消息不为空
int command = msgRecvQueue.front(); //返回第一个元素,但不检查元素是否存在;
msgRecvQueue.pop_front(); //移除第一个元素,但不返回
//这里就考虑处理数据...
//...
}
else
{
//消息队列为空
cout << "outMsgRecvQueue()执行,但目前消息队列中为空 " << i << endl;
}
}
cout << "end" << endl;
}
private:
list<int> msgRecvQueue; //容器(消息队列),专门用于代表玩家发送的命令
};
int main()
{
MA myobj;
std::thread myOutMsgObj(&MA::outMsgRecvQueue, &myobj); //第二个参数是引用,才能保证线程里用的是同一个对象
std::thread myInMsgObj(&MA::inMsgRecvQueue, &myobj);
myOutMsgObj.join();
myInMsgObj.join();
cout << "main主函数执行结束" << endl; //最后执行这句,整个进程退出
return 0;
}
代码化解决问题:引入一个 c++ 解决多线程保护共享数据问题的第一个概念"互斥量",往脑袋里记这个词。
边栏推荐
- Report on various activity plans of safety month 2022 (28 pages)
- Iterator mode -- battlefield autumn point
- 数字藏品系统app源码
- 1712. 将数组分成三个子数组的方案数 ●●
- NFT will change data ownership in the metauniverse
- 使用Labelimg制作VOC数据集或yolo数据集的入门方法
- MySQL optimized learning diary 10 - locking mechanism
- Distance measurement - Euclidean distance
- 想做钢铁侠?听说很多大佬都是用它入门的
- 企业微信小程序避坑指南,欢迎补充。。。
猜你喜欢

施一公:我直到博士毕业,对研究也没兴趣!对未来很迷茫,也不知道将来要干什么......

杰理之获取 BLE 区分复位跟唤醒【篇】

Distance measurement - Euclidean distance

Introduction to thread pool: ThreadPoolExecutor

MySQL optimized learning diary 10 - locking mechanism

985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci

Appearance mode -- it has been used in various packages for a long time!

VOC格式数据集转yolo格式数据集的方法

AI security and Privacy Forum issue 11 - stable learning: finding common ground between causal reasoning and machine learning

Don't be a fake worker
随机推荐
C language campus tour guide consultation system
Content-Type: multipart/form-data; boundary=${bound}
Content-Type: multipart/form-data; boundary=${bound}
Unity font spacing
拆分数据---水平拆分和纵向拆分
数据库系统概论 ---- 第二章 -- 关系数据库(2.4 关系代数)
李飞飞:我更像物理学界的科学家,而不是工程师|深度学习崛起十年
杰理之BLE 芯片供电范围及防烧芯片措施【篇】
Electron desktop development (development of an alarm clock [End])
An introduction to creating VOC datasets or Yolo datasets using labelimg
杰理之BLE SPP 开启 pin_code 功能【篇】
Iterator mode -- battlefield autumn point
CAP理论听起来很高大上,其实很简单
2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
Cube 技术解读 | Cube 渲染设计的前世今生
找到自己的优势,才能干活不累,事半功倍!
AcWing 1353. 滑雪场设计(贪心)
Introduction to database system - Chapter 2 - relational database (2.1~2.3) (important knowledge points)
Package component series - (I) - slots and dynamic components
Summary of English thesis reading knowledge