C++ 11使用thread类多线程编程
转自https://blog.csdn.net/dcrmg/article/details/53912941
C++11中引入了一个用于多线程操作的thread类,简单多线程示例:
- #include <iostream>
- #include <thread>
- #include <Windows.h>
- using namespace std;
- void thread01()
- {
- for (int i = 0; i < 5; i++)
- {
- cout << "Thread 01 is working !" << endl;
- Sleep(100);
- }
- }
- void thread02()
- {
- for (int i = 0; i < 5; i++)
- {
- cout << "Thread 02 is working !" << endl;
- Sleep(200);
- }
- }
- int main()
- {
- thread task01(thread01);
- thread task02(thread02);
- task01.join();
- task02.join();
- for (int i = 0; i < 5; i++)
- {
- cout << "Main thread is working !" << endl;
- Sleep(200);
- }
- system("pause");
- }
输出:
两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程。可以使用detach将子线程从主流程中分离,独立运行,不会阻塞主线程:
- #include <iostream>
- #include <thread>
- #include <Windows.h>
- using namespace std;
- void thread01()
- {
- for (int i = 0; i < 5; i++)
- {
- cout << "Thread 01 is working !" << endl;
- Sleep(100);
- }
- }
- void thread02()
- {
- for (int i = 0; i < 5; i++)
- {
- cout << "Thread 02 is working !" << endl;
- Sleep(200);
- }
- }
- int main()
- {
- thread task01(thread01);
- thread task02(thread02);
- task01.detach();
- task02.detach();
- for (int i = 0; i < 5; i++)
- {
- cout << "Main thread is working !" << endl;
- Sleep(200);
- }
- system("pause");
- }
使用detach的主线程和两个子线程并行执行。
带参子线程
在绑定的时候也可以同时给带参数的线程传入参数:
- #include <iostream>
- #include <thread>
- #include <Windows.h>
- using namespace std;
- //定义带参数子线程
- void thread01(int num)
- {
- for (int i = 0; i < num; i++)
- {
- cout << "Thread 01 is working !" << endl;
- Sleep(100);
- }
- }
- void thread02(int num)
- {
- for (int i = 0; i < num; i++)
- {
- cout << "Thread 02 is working !" << endl;
- Sleep(200);
- }
- }
- int main()
- {
- thread task01(thread01, 5); //带参数子线程
- thread task02(thread02, 5);
- task01.detach();
- task02.detach();
- for (int i = 0; i < 5; i++)
- {
- cout << "Main thread is working !" << endl;
- Sleep(200);
- }
- system("pause");
- }
输出跟上例输出一样:
多线程数据竞争
多个线程同时对同一变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:
- #include <iostream>
- #include <thread>
- #include <Windows.h>
- using namespace std;
- int totalNum = 100;
- void thread01()
- {
- while (totalNum > 0)
- {
- cout << totalNum << endl;
- totalNum--;
- Sleep(100);
- }
- }
- void thread02()
- {
- while (totalNum > 0)
- {
- cout << totalNum << endl;
- totalNum--;
- Sleep(100);
- }
- }
- int main()
- {
- thread task01(thread01);
- thread task02(thread02);
- task01.detach();
- task02.detach();
- system("pause");
- }
输出结果(部分):
有两个问题,一是有很多变量被重复输出了,而有的变量没有被输出;二是正常情况下每个线程输出的数据后应该紧跟一个换行符,但这里大部分却是另一个线程的输出。
这是由于第一个线程对变量操作的过程中,第二个线程也对同一个变量进行各操作,导致第一个线程处理完后的输出有可能是线程二操作的结果。针对这种数据竞争的情况,可以使用线程互斥对象mutex保持数据同步。
mutex类的使用需要包含头文件mutex:
- #include <iostream>
- #include <thread>
- #include <Windows.h>
- #include <mutex>
- using namespace std;
- mutex mu; //线程互斥对象
- int totalNum = 100;
- void thread01()
- {
- while (totalNum > 0)
- {
- mu.lock(); //同步数据锁
- cout << totalNum << endl;
- totalNum--;
- Sleep(100);
- mu.unlock(); //解除锁定
- }
- }
- void thread02()
- {
- while (totalNum > 0)
- {
- mu.lock();
- cout << totalNum << endl;
- totalNum--;
- Sleep(100);
- mu.unlock();
- }
- }
- int main()
- {
- thread task01(thread01);
- thread task02(thread02);
- task01.detach();
- task02.detach();
- system("pause");
- }
多线程中加入mutex互斥对象之后输出正常:
C++ 11使用thread类多线程编程相关推荐
- C++使用thread类多线程编程
目录 pthread多线程 系统自带CreateThread std::thread c++ 多线程总结_jacke121的专栏-CSDN博客 std thread比较好用,但是系统带的socket不 ...
- C++11中Thread类简单使用的例子
代码如下: #include <iostream> #include <thread> #include <chrono> #include <future& ...
- C 多线程编程之在类中使用多线程(thread)的方法
一.thread的基本用法 参见C++使用thread类多线程编程 . 二.类外使用多线程,访问类的成员 这几种方式,新建线程都是在类外,然后通过把友元函数或者成员函数作为thread参数. #inc ...
- [C++11 std::thread] 使用C++11 编写 Linux 多线程程序
From: http://www.ibm.com/developerworks/cn/linux/1412_zhupx_thread/index.html 本文讲述了如何使用 C++11 编写 Lin ...
- c++11 thread类的简单使用
一个thread对象就代表一个线程执行,当对象创建完成后,线程函数就开始执行,下面这段话是c++11的thread类介绍. An initialized thread object represent ...
- Linux多线程编程---线程间同步(互斥锁、条件变量、信号量和读写锁)
本篇博文转自http://zhangxiaoya.github.io/2015/05/15/multi-thread-of-c-program-language-on-linux/ Linux下提供了 ...
- Android中的多线程编程与异步处理
Android中的多线程编程与异步处理 引言 在移动应用开发中,用户体验是至关重要的.一个流畅.高效的应用能够吸引用户并提升用户满意度.然而,移动应用面临着处理复杂业务逻辑.响应用户输入.处理网络请求 ...
- Linux环境下,C++实现龟兔赛跑的多线程编程
编程 使⽤ Thread 实现多线程编程 题目:龟兔赛跑,跑道距离50米.乌龟(作为⼀个线程) 每秒3米,不睡觉 : 兔子(作为⼀个线程)每秒5米,每跑15⽶睡2秒钟.请模拟比赛情况. #includ ...
- c#.net多线程编程教学(2):Thread类
这章将向大家介绍.NET中的线程API,怎么样用C#创建线程,启动和停止线程,设置优先级和状态. 在.NET中编写的程序将被自动的分配一个线程.让我们来看看用C#编程语言创建线程并且继续学习线程的知识 ...
最新文章
- [Android] 获取设备的APP信息
- html and js 的隔行换背景色表格实例详解
- n阶换方c语言程序,求单偶阶与双偶阶幻方编程思想及其算法!
- 前端学习(2473):创建页面组件
- Spring boot中使用log4j记录日志
- 代码分析 | 单细胞转录组Normalization详解
- Vue 调试工具 vue-devtools 安装及使用
- android给图片加边框,Android学习笔记进阶19之给图片加边框
- 是什么原因让你选择做程序员
- 电子表格和oracle,##请问数据库和电子表格之间有什么区别
- 拓端tecdat|R语言贝叶斯Poisson泊松-正态分布模型分析职业足球比赛进球数
- Tomcat发布项目时,浏览器地址栏图标的问题
- SAP生产模式和计划策略
- 2015 沈阳 Frogs
- 微信中无法下载APP的解决办法
- 阿里云的WEB应用防火墙(WAF)能抵挡什么攻击
- Bootstrap4 图片形状——圆形实现
- 接口测试实战| GET/POST 请求区别详解
- 如何使错误日志更加方便排查问题?
- R语言使用逻辑回归分类算法
热门文章
- SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易
- [转载] C语言C++指针与java中引用的一点对此
- XILINX ZYNQ7100 的上电顺序
- 用LinkedList方法模拟栈的数据结构
- python3 验证用户名密码
- Wildcard Matching
- SLAM的一些基础知识
- mysql存储过程更新数据后返回一个字段_史上最全存储引擎、索引使用及SQL优化的实践...
- ansys icem cfd网格划分技术实例详解_详解航空燃油滑油3D打印热交换器设计流程...
- java知识点博客园_JAVA基础知识回顾