转自https://blog.csdn.net/dcrmg/article/details/53912941

C++11中引入了一个用于多线程操作的thread类,简单多线程示例:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <thread>
  3. #include <Windows.h>
  4. using namespace std;
  5. void thread01()
  6. {
  7. for (int i = 0; i < 5; i++)
  8. {
  9. cout << "Thread 01 is working !" << endl;
  10. Sleep(100);
  11. }
  12. }
  13. void thread02()
  14. {
  15. for (int i = 0; i < 5; i++)
  16. {
  17. cout << "Thread 02 is working !" << endl;
  18. Sleep(200);
  19. }
  20. }
  21. int main()
  22. {
  23. thread task01(thread01);
  24. thread task02(thread02);
  25. task01.join();
  26. task02.join();
  27. for (int i = 0; i < 5; i++)
  28. {
  29. cout << "Main thread is working !" << endl;
  30. Sleep(200);
  31. }
  32. system("pause");
  33. }

输出:

两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程。可以使用detach将子线程从主流程中分离,独立运行,不会阻塞主线程:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <thread>
  3. #include <Windows.h>
  4. using namespace std;
  5. void thread01()
  6. {
  7. for (int i = 0; i < 5; i++)
  8. {
  9. cout << "Thread 01 is working !" << endl;
  10. Sleep(100);
  11. }
  12. }
  13. void thread02()
  14. {
  15. for (int i = 0; i < 5; i++)
  16. {
  17. cout << "Thread 02 is working !" << endl;
  18. Sleep(200);
  19. }
  20. }
  21. int main()
  22. {
  23. thread task01(thread01);
  24. thread task02(thread02);
  25. task01.detach();
  26. task02.detach();
  27. for (int i = 0; i < 5; i++)
  28. {
  29. cout << "Main thread is working !" << endl;
  30. Sleep(200);
  31. }
  32. system("pause");
  33. }
输出:

使用detach的主线程和两个子线程并行执行。

带参子线程

在绑定的时候也可以同时给带参数的线程传入参数:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <thread>
  3. #include <Windows.h>
  4. using namespace std;
  5. //定义带参数子线程
  6. void thread01(int num)
  7. {
  8. for (int i = 0; i < num; i++)
  9. {
  10. cout << "Thread 01 is working !" << endl;
  11. Sleep(100);
  12. }
  13. }
  14. void thread02(int num)
  15. {
  16. for (int i = 0; i < num; i++)
  17. {
  18. cout << "Thread 02 is working !" << endl;
  19. Sleep(200);
  20. }
  21. }
  22. int main()
  23. {
  24. thread task01(thread01, 5);  //带参数子线程
  25. thread task02(thread02, 5);
  26. task01.detach();
  27. task02.detach();
  28. for (int i = 0; i < 5; i++)
  29. {
  30. cout << "Main thread is working !" << endl;
  31. Sleep(200);
  32. }
  33. system("pause");
  34. }

输出跟上例输出一样:

多线程数据竞争

多个线程同时对同一变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <thread>
  3. #include <Windows.h>
  4. using namespace std;
  5. int totalNum = 100;
  6. void thread01()
  7. {
  8. while (totalNum > 0)
  9. {
  10. cout << totalNum << endl;
  11. totalNum--;
  12. Sleep(100);
  13. }
  14. }
  15. void thread02()
  16. {
  17. while (totalNum > 0)
  18. {
  19. cout << totalNum << endl;
  20. totalNum--;
  21. Sleep(100);
  22. }
  23. }
  24. int main()
  25. {
  26. thread task01(thread01);
  27. thread task02(thread02);
  28. task01.detach();
  29. task02.detach();
  30. system("pause");
  31. }

输出结果(部分):

有两个问题,一是有很多变量被重复输出了,而有的变量没有被输出;二是正常情况下每个线程输出的数据后应该紧跟一个换行符,但这里大部分却是另一个线程的输出。

这是由于第一个线程对变量操作的过程中,第二个线程也对同一个变量进行各操作,导致第一个线程处理完后的输出有可能是线程二操作的结果。针对这种数据竞争的情况,可以使用线程互斥对象mutex保持数据同步。

mutex类的使用需要包含头文件mutex:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <thread>
  3. #include <Windows.h>
  4. #include <mutex>
  5. using namespace std;
  6. mutex mu;  //线程互斥对象
  7. int totalNum = 100;
  8. void thread01()
  9. {
  10. while (totalNum > 0)
  11. {
  12. mu.lock(); //同步数据锁
  13. cout << totalNum << endl;
  14. totalNum--;
  15. Sleep(100);
  16. mu.unlock();  //解除锁定
  17. }
  18. }
  19. void thread02()
  20. {
  21. while (totalNum > 0)
  22. {
  23. mu.lock();
  24. cout << totalNum << endl;
  25. totalNum--;
  26. Sleep(100);
  27. mu.unlock();
  28. }
  29. }
  30. int main()
  31. {
  32. thread task01(thread01);
  33. thread task02(thread02);
  34. task01.detach();
  35. task02.detach();
  36. system("pause");
  37. }

多线程中加入mutex互斥对象之后输出正常:

C++ 11使用thread类多线程编程相关推荐

  1. C++使用thread类多线程编程

    目录 pthread多线程 系统自带CreateThread std::thread c++ 多线程总结_jacke121的专栏-CSDN博客 std thread比较好用,但是系统带的socket不 ...

  2. C++11中Thread类简单使用的例子

    代码如下: #include <iostream> #include <thread> #include <chrono> #include <future& ...

  3. C 多线程编程之在类中使用多线程(thread)的方法

    一.thread的基本用法 参见C++使用thread类多线程编程 . 二.类外使用多线程,访问类的成员 这几种方式,新建线程都是在类外,然后通过把友元函数或者成员函数作为thread参数. #inc ...

  4. [C++11 std::thread] 使用C++11 编写 Linux 多线程程序

    From: http://www.ibm.com/developerworks/cn/linux/1412_zhupx_thread/index.html 本文讲述了如何使用 C++11 编写 Lin ...

  5. c++11 thread类的简单使用

    一个thread对象就代表一个线程执行,当对象创建完成后,线程函数就开始执行,下面这段话是c++11的thread类介绍. An initialized thread object represent ...

  6. Linux多线程编程---线程间同步(互斥锁、条件变量、信号量和读写锁)

    本篇博文转自http://zhangxiaoya.github.io/2015/05/15/multi-thread-of-c-program-language-on-linux/ Linux下提供了 ...

  7. Android中的多线程编程与异步处理

    Android中的多线程编程与异步处理 引言 在移动应用开发中,用户体验是至关重要的.一个流畅.高效的应用能够吸引用户并提升用户满意度.然而,移动应用面临着处理复杂业务逻辑.响应用户输入.处理网络请求 ...

  8. Linux环境下,C++实现龟兔赛跑的多线程编程

    编程 使⽤ Thread 实现多线程编程 题目:龟兔赛跑,跑道距离50米.乌龟(作为⼀个线程) 每秒3米,不睡觉 : 兔子(作为⼀个线程)每秒5米,每跑15⽶睡2秒钟.请模拟比赛情况. #includ ...

  9. c#.net多线程编程教学(2):Thread类

    这章将向大家介绍.NET中的线程API,怎么样用C#创建线程,启动和停止线程,设置优先级和状态. 在.NET中编写的程序将被自动的分配一个线程.让我们来看看用C#编程语言创建线程并且继续学习线程的知识 ...

最新文章

  1. [Android] 获取设备的APP信息
  2. html and js 的隔行换背景色表格实例详解
  3. n阶换方c语言程序,求单偶阶与双偶阶幻方编程思想及其算法!
  4. 前端学习(2473):创建页面组件
  5. Spring boot中使用log4j记录日志
  6. 代码分析 | 单细胞转录组Normalization详解
  7. Vue 调试工具 vue-devtools 安装及使用
  8. android给图片加边框,Android学习笔记进阶19之给图片加边框
  9. 是什么原因让你选择做程序员
  10. 电子表格和oracle,##请问数据库和电子表格之间有什么区别
  11. 拓端tecdat|R语言贝叶斯Poisson泊松-正态分布模型分析职业足球比赛进球数
  12. Tomcat发布项目时,浏览器地址栏图标的问题
  13. SAP生产模式和计划策略
  14. 2015 沈阳 Frogs
  15. 微信中无法下载APP的解决办法
  16. 阿里云的WEB应用防火墙(WAF)能抵挡什么攻击
  17. Bootstrap4 图片形状——圆形实现
  18. 接口测试实战| GET/POST 请求区别详解
  19. 如何使错误日志更加方便排查问题?
  20. R语言使用逻辑回归分类算法

热门文章

  1. SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易
  2. [转载] C语言C++指针与java中引用的一点对此
  3. XILINX ZYNQ7100 的上电顺序
  4. 用LinkedList方法模拟栈的数据结构
  5. python3 验证用户名密码
  6. Wildcard Matching
  7. SLAM的一些基础知识
  8. mysql存储过程更新数据后返回一个字段_史上最全存储引擎、索引使用及SQL优化的实践...
  9. ansys icem cfd网格划分技术实例详解_详解航空燃油滑油3D打印热交换器设计流程...
  10. java知识点博客园_JAVA基础知识回顾