C++ thread类多线程编程
https://blog.csdn.net/dcrmg/article/details/53912941
多线程操作的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");
}
输出:
Thread 02 is working !Thread 01 is working !
Thread 01 is working !
Thread 02 is working !
Thread 01 is working !
Thread 01 is working !
Thread 02 is working !
Thread 01 is working !
Thread 02 is working !
Thread 02 is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
请按任意键继续. . .
两个子线程并行执行,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");
}
输出:
Main thread is working !
Thread 01 is working !Thread 02 is working !
Thread 01 is working !
Thread 02 is working !Main thread is working !
Thread 01 is working !
Thread 01 is working !
Thread 02 is working !Main thread is working !
Thread 01 is working !
Main thread is working !Thread 02 is working !
Main thread is working !Thread 02 is working !
请按任意键继续. . .
用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++ thread类多线程编程的更多相关文章
- C++使用thread类多线程编程
转自:C++使用thread类多线程编程 C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步. thread简单示例 #include <io ...
- 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类中 ...
随机推荐
- selenium 安装 以及相关环境
在cmd中安装简单, pip install selenium 一键安装 如果需要 chromedriver 还需要安装相对应的 版本 看到网上基本没有最新的chromedriver与chr ...
- XamarinSQLite教程在Xamarin.Android项目中定位数据库文件
XamarinSQLite教程在Xamarin.Android项目中定位数据库文件 实际开发中,经常需要验证数据库操作的正确性.这个时候,需要打开数据库文件,进行确认.下面是如何找到MyDocumen ...
- 【Intellij IDEA】eclipse项目导入
intellij idea中文资料网上比较少,对于eclipse的项目如何导入intellij idea也没有完整的说明,本人在这里整理下,方便更多人加入到intellij idea的阵容里. 直接上 ...
- 在UnrealEngine中用Custom节点实现高斯模糊
3x3高斯模糊 //input sW 分辨率宽 //input sH 分辨率高 //input NotUse 为了开启SceneTextureLookup函数而连接的节点,但是不参与逻辑 //inpu ...
- CODEVS 2455 繁忙的都市 SCOI2005(洛谷 P2330)
题目描述 Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两 ...
- JavaScript基础笔记(七)DOM
DOM DOM可以将任何HTML或者XML文档描述成一个由多层节点构成的结构. 一.节点层次 一)Node类型 DOM1定义了一个Node接口,该接口将由DOM中所有节点类型实现. 每一个节点都有一个 ...
- nodejs,mongodb不同时区问题
问题:不同国家,使用不同时区,而服务器代码却在国内,跨时区日期不同,根据日期查询,查询不到数据了 1.mongodb存储的new Date()是UTC时间,也就是0时区的时间,世界标准时间 2.参考m ...
- BZOJ4964 : 加长的咒语
把$($看作$-1$,$)$看作$1$,设$a$为前缀和,则相当于找两个位置$x,y$使得$a[x]=a[y]$,且$a[x]$是$[x,y]$的区间最大值. 求出询问区间的最大值$o$,然后找到$o ...
- 2017.07.10【NOIP提高组】模拟赛B组
Summary 今天题目总体不是难,但是分数很低,只有100+10+30,其中第二题还是以前做过的,第一题设计数论,而且以前做过同一个类型的题目,比赛推了很长时间.第三题时以前做过的原题,是贪心没学好 ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习8
#include <iostream>using namespace std;const int Size=20;struct Pizza{ char company[Size]; dou ...