C++使用thread类多线程编程
C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步。
thread简单示例
#include <iostream>
#include <thread>
#include <Windows.h> using namespace std; void thread01()
{
for (int i = ; i < ; i++)
{
cout << "Thread 01 is working !" << endl;
Sleep();
}
}
void thread02()
{
for (int i = ; i < ; i++)
{
cout << "Thread 02 is working !" << endl;
Sleep();
}
} int main()
{
thread task01(thread01);
thread task02(thread02);
task01.join();
task02.join(); for (int i = ; i < ; i++)
{
cout << "Main thread is working !" << endl;
Sleep();
}
system("pause");
}
输出:

thread detach不阻塞主线程
两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程。可以使用detach将子线程从主流程中分离,独立运行,不会阻塞主线程:
#include <iostream>
#include <thread>
#include <Windows.h> using namespace std; void thread01()
{
for (int i = ; i < ; i++)
{
cout << "Thread 01 is working !" << endl;
Sleep();
}
}
void thread02()
{
for (int i = ; i < ; i++)
{
cout << "Thread 02 is working !" << endl;
Sleep();
}
} int main()
{
thread task01(thread01);
thread task02(thread02);
task01.detach();
task02.detach(); for (int i = ; i < ; i++)
{
cout << "Main thread is working !" << endl;
Sleep();
}
system("pause");
}
输出:

使用detach的主线程和两个子线程并行执行。
thread带参数子线程
在绑定的时候也可以同时给带参数的线程传入参数:
#include <iostream>
#include <thread>
#include <Windows.h> using namespace std; //定义带参数子线程
void thread01(int num)
{
for (int i = ; i < num; i++)
{
cout << "Thread 01 is working !" << endl;
Sleep();
}
}
void thread02(int num)
{
for (int i = ; i < num; i++)
{
cout << "Thread 02 is working !" << endl;
Sleep();
}
} int main()
{
thread task01(thread01, ); //带参数子线程
thread task02(thread02, );
task01.detach();
task02.detach(); for (int i = ; i < ; i++)
{
cout << "Main thread is working !" << endl;
Sleep();
}
system("pause");
}
输出:

多线程同步mutex
多个线程同时对同一变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:
#include <iostream>
#include <thread>
#include <Windows.h> using namespace std; int totalNum = ; void thread01()
{
while (totalNum > )
{
cout << totalNum << endl;
totalNum--;
Sleep();
}
}
void thread02()
{
while (totalNum > )
{
cout << totalNum << endl;
totalNum--;
Sleep();
}
} 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 = ; void thread01()
{
while (totalNum > )
{
mu.lock(); //同步数据锁
cout << totalNum << endl;
totalNum--;
Sleep();
mu.unlock(); //解除锁定
}
}
void thread02()
{
while (totalNum > )
{
mu.lock();
cout << totalNum << endl;
totalNum--;
Sleep();
mu.unlock();
}
} int main()
{
thread task01(thread01);
thread task02(thread02);
task01.detach();
task02.detach();
system("pause");
}
多线程中加入mutex互斥对象之后输出正常:

C++使用thread类多线程编程的更多相关文章
- C++ thread类多线程编程
https://blog.csdn.net/dcrmg/article/details/53912941 多线程操作的thread类,简单多线程示例: #include <iostream> ...
- thread模块—Python多线程编程
Thread 模块 *注:在实际使用过程中不建议使用 thread 进行多线程编程,本文档只为学习(或熟悉)多线程使用. Thread 模块除了派生线程外,还提供了基本的同步数据结构,称为锁对象(lo ...
- C++使用thread类进行多线程编程
C++11中引入了一个用于多线程操作的thread类,简单多线程示例: #include <iostream> #include <thread> #include <W ...
- Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...
- 实验六 多线程编程 1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
//继承Thread类 package zuoye; //继承Thread类 public class City extends Thread{ private String name; public ...
- Java并发编程:Thread类的使用
Java并发编程:Thread类的使用 在前面2篇文章分别讲到了线程和进程的由来.以及如何在Java中怎么创建线程和进程.今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知 ...
- Delphi中线程类TThread实现多线程编程1---构造、析构……
参考:http://www.cnblogs.com/rogee/archive/2010/09/20/1832053.html Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大 ...
- C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程
创建线程的常用方法:异步委托.定时器.Thread类 理解程序.进程.线程三者之间的区别:简而言之,一个程序至少有一个进程,一个进程至少有一个线程进程就是在内存中运行的程序(即运行着的程序):一个进程 ...
- Java并发编程:Thread类的使用介绍
在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着介绍Thread类中的方法的具体使用. 以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中 ...
随机推荐
- java FTPClient 上传文件 0kb 问题
解决方法: 1.本地防火墙关闭了2.服务端端防火墙关闭 CentOS 7.0关闭默认防火墙启用iptables防火墙 操作系统环境:CentOS Linux release 7.0.1406(Core ...
- 容器平台选型的十大模式:Docker、DC/OS、K8S 谁与当先?【转】
网易企业服务2017-10-13 无论是在社区,还是在同客户交流的过程中,总会被问到到底什么时候该用 Docker?什么时候用虚拟机?如果使用容器,应该使用哪个容器平台? 显而易见,我不会直接给大家一 ...
- OneNET麒麟座应用开发之二:串口读取PM25传感器数据
作为环境数据监测站首先要获取大气中可吸入颗粒物的数据.为了检测PM25数据,我们采用北京海联信为的HLPM025K3型号传感器,该传感器使用激光法测量PM25和PM10的数据. 该型传感器的检测对象如 ...
- 选择一个 HTTP 状态码不再是一件难事 – Racksburg《转载》
本文转载自:众成翻译 译者:十年踪迹 链接:http://www.zcfy.cc/article/904 原文:http://racksburg.com/choosing-an-http-status ...
- Jmeter接口测试实例图文示例
以getObjectByCode接口为例,用jmeter2.13来进行接口测试. 测试前准备: 测试工具及版本:jmeter 2.13 r1665067(须包含__MD5函数) 示例接口:8.1根据单 ...
- SeaJS入门教程系列之完整示例(三)
一个完整的例子上文说了那么多,知识点比较分散,所以最后我打算用一个完整的SeaJS例子把这些知识点串起来,方便朋友们归纳回顾.这个例子包含如下文件: 1.index.html——主页面.2.sea.j ...
- AdvStringGrid 标题头 加粗的问题
当AdvStringGrid1.RowCount = 1的时候,会是下面这样: 当AdvStringGrid1.RowCount = 2 时 才是正确的:
- python3笔记(三)if...else、if...elif...else
if语句 if 语句的基本用法如下: if 表达式: 语句块 其中,表达式可以是一个单纯的布尔值或变量,也可以是比较表达式或逻辑表达式(例 如:a > band a != c),如果表达式为真, ...
- Javascript中的函数(三)
一:概述 函数是进行模块化程序设计的基础,编写复杂的Ajax应用程序,必须对函数有更深入的了解.JavaScript中的函数不同于其他的语言,每个函数都是作为一个对象被维护和运行的.通过函数对象的性质 ...
- 【C++ Primer 第10章】 10.4.2 插入迭代器
iostream迭代器 标准库为iostream定义了可用于这些IO类型对象的迭代器. istream_iterator读取输入流, ostream_iterator向一个输出流写数据. 1. i ...