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类多线程编程的更多相关文章

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

    转自:C++使用thread类多线程编程 C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步. thread简单示例 #include <io ...

  2. thread模块—Python多线程编程

    Thread 模块 *注:在实际使用过程中不建议使用 thread 进行多线程编程,本文档只为学习(或熟悉)多线程使用. Thread 模块除了派生线程外,还提供了基本的同步数据结构,称为锁对象(lo ...

  3. C++使用thread类进行多线程编程

    C++11中引入了一个用于多线程操作的thread类,简单多线程示例: #include <iostream> #include <thread> #include <W ...

  4. Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...

  5. 实验六 多线程编程 1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    //继承Thread类 package zuoye; //继承Thread类 public class City extends Thread{ private String name; public ...

  6. Java并发编程:Thread类的使用

    Java并发编程:Thread类的使用 在前面2篇文章分别讲到了线程和进程的由来.以及如何在Java中怎么创建线程和进程.今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知 ...

  7. Delphi中线程类TThread实现多线程编程1---构造、析构……

    参考:http://www.cnblogs.com/rogee/archive/2010/09/20/1832053.html Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大 ...

  8. C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程

    创建线程的常用方法:异步委托.定时器.Thread类 理解程序.进程.线程三者之间的区别:简而言之,一个程序至少有一个进程,一个进程至少有一个线程进程就是在内存中运行的程序(即运行着的程序):一个进程 ...

  9. Java并发编程:Thread类的使用介绍

    在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着介绍Thread类中的方法的具体使用. 以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中 ...

随机推荐

  1. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

  2. ACM10.14题解

    ACM10.14题解 第一次打周赛,感觉还是比较紧张的,应该开完所有的题再做,而不是硬做,没必要硬杠英语,还是不要抱有侥幸心理,做对一定是完全理解且会,自己小心边界问题,不要瞎交. A:暴力明显不对嘛 ...

  3. flex 布局的复习

  4. XamarinAndroid组件教程RecylerView适配器设置动画

    XamarinAndroid组件教程RecylerView适配器设置动画 本小节将讲解动画相关设置,如动画的时长.插值器以及复合动画等. 1.设置动画时长 设置动画持续的时间可以使用Animation ...

  5. ubuntu16 64 搭建lnmp环境

    //安全设置linux(ubuntu16 64) 安全设置1.修改ssh端口 vi /etc/ssh/sshd_config 如果用户想让22和60000端口同时开放,只需在/etc/ssh/sshd ...

  6. BZOJ2670 : Almost

    求出前缀和$s[]$,那么区间$[l,r]$的几乎平均数$=\frac{s[r]-s[l-1]}{r-l}$. 若只有一个询问,那么可以维护$(i,s[i-1])$的凸壳,在凸壳上二分点$(i,s[i ...

  7. BKDR Hash 函数实现

    K&R一书中提出的BKDR Hash算法,这里给出C函数实现,实际用的时候分布比较好而且实现简单.唯一不明白的就是为什么选择131这种模式的数字作为种子,隐隐有沃尔夫勒姆31号自动机的似曾相识 ...

  8. innerHTML innerText与outerHTML间的区别

    innerHTML与innerText及outerHTML间的区别最容易使初学者搞混淆,为了更好的使读者区分开.下面我就通过一个demo来解释: 代码: <!DOCTYPE html>&l ...

  9. jQuery如何追加tr到table中任意位置--向Table中指定位置添加tr或td(jQuery)

    jQuery 添加新内容有以下四个方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() ...

  10. day3字典_字符串_文件操作

    一.知识回顾 1.jmeter怎样增加压力机:在主控机配置文件 jmeter.perperties 文件中添加对应压力的IP和对应端口,多台压力机IP用","隔开,保证所有负载机和 ...