C++11并发编程:多线程std::thread
一:概述
C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改。现在在C++11中只需使用语言层面的thread可以解决这个问题。
所需头文件<thread>
二:构造函数
1.默认构造函数
thread() noexcept
一个空的std::thread执行对象
2.初始化构造函数
template<class Fn, class... Args>
explicit thread(Fn&& fn, Args&&... args);
创建std::thread执行对象,线程调用threadFun函数,函数参数为args。
1 void threadFun(int a) 2 { 3 cout << "this is thread fun !" << endl; 4 } 5 6 thread t1(threadFun, 2);
3.拷贝构造函数
thread(const thread&) = delete;
拷贝构造函数被禁用,std::thread对象不可拷贝构造
1 void threadFun(int& a) 2 { 3 cout << "this is thread fun !" << endl; 4 } 5 6 int value = 2; 7 thread t1(threadFun, std::ref(value));
4.Move构造函数
thread(thread&& x)noexcept
调用成功原来x不再是std::thread对象
1 void threadFun(int& a) 2 { 3 cout << "this is thread fun !" << endl; 4 } 5 6 int value = 2; 7 thread t1(threadFun, std::ref(value)); 8 thread t2(std::move(t1)); 9 t2.join();
三:成员函数
1.get_id()
获取线程ID,返回类型std::thread::id对象。
1 thread t1(threadFun); 2 thread::id threadId = t1.get_id(); 3 cout << "线程ID:" << threadId << endl; 4 5 //threadId转换成整形值,所需头文件<sstream> 6 ostringstream oss; 7 oss << t1.get_id(); 8 string strId = oss.str(); 9 unsigned long long tid = stoull(strId); 10 cout << "线程ID:" << tid << endl;
2.join()
创建线程执行线程函数,调用该函数会阻塞当前线程,直到线程执行完join才返回。
thread t1(threadFun); t1.join() //阻塞等待
3.detach()
detach调用之后,目标线程就成为了守护线程,驻留后台运行,与之关联的std::thread对象失去对目标线程的关联,无法再通过std::thread对象取得该线程的控制权。
4.swap()
交换两个线程对象
1 thread t1(threadFun1); 2 thread t2(threadFun2); 3 cout << "线程1的ID:" << t1.get_id() << endl; 4 cout << "线程2的ID:" << t2.get_id() << endl; 5 6 t1.swap(t2); 7 8 cout << "线程1的ID:" << t1.get_id() << endl; 9 cout << "线程2的ID:" << t2.get_id() << endl;
5.hardware_concurrency()
获得逻辑处理器储量,返回值为int型
1 int coreNum = thread::hardware_concurrency();
四:使用
1.创建线程
1 void threadFun1() 2 { 3 cout << "this is thread fun1 !" << endl; 4 } 5 6 int main() 7 { 8 thread t1(threadFun1); 9 t1.join(); 10 11 getchar(); 12 return 1; 13 }
2.创建线程,传参
1 void threadFun1(int v) 2 { 3 cout << "this is thread fun1 !" << endl; 4 cout << v << endl; 5 } 6 7 int main() 8 { 9 int value = 6; 10 thread t1(threadFun1, value); 11 t1.join(); 12 13 getchar(); 14 return 1; 15 }
需要注意,变量int value 和int v 做变量传递时并不是引用,而是对变量做了拷贝,所以在传递给int v前,int value不能出作用域(释放了内存),join(),可以保证int value变量释放内存,如果使用detach(),可能存在这种情况。
3.创建线程,引用传参
1 void threadFun1(int& v) 2 { 3 cout << "this is thread fun1 !" << endl; 4 cout << v << endl; 5 } 6 7 int main() 8 { 9 int value = 6; 10 thread t1(threadFun1, std::ref(value)); 11 t1.join(); 12 13 getchar(); 14 return 1; 15 }
4.创建建线程,线程函数为类成员函数
1 class Object 2 { 3 public: 4 Object() 5 { 6 cout << "构造函数" << endl; 7 } 8 9 ~Object() 10 { 11 cout << "析构函数" << endl; 12 } 13 14 void fun(string info) 15 { 16 cout << info << endl; 17 } 18 19 }; 20 21 int main() 22 { 23 24 Object obj; 25 string str = "我是一个类的成员函数!"; 26 thread t1(&Object::fun, &obj, str); 27 t1.join(); 28 29 getchar(); 30 return 1; 31 }
扫码关注公众号
专注分享C/C++,C++(11,14,17),STL,Java,Spring,mybatis,mysql,redis,分布式,高并发,设计模式,爬虫,docker,shell编程等相关技术,还有高薪互联网职位内推,在这里一起探讨,一起学习,一起进步,同时不定期分享视频书籍资源,充分利用碎片化时间,让我们的技术之路更加有乐趣!
转载于:https://www.cnblogs.com/woniu201/p/10145044.html
C++11并发编程:多线程std::thread相关推荐
- 【C/C++开发】C++11 并发指南二(std::thread 详解)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- C++11 并发指南二(std::thread 详解)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- 【转】C++11 并发指南五(std::condition_variable 详解)
http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...
- C++11 并发指南五(std::condition_variable 详解)
前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...
- C++11 并发指南三(std::mutex 详解)
上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...
- c+++11并发编程语言,C++11并发编程:多线程std:thread
原标题:C++11并发编程:多线程std:thread 一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植, ...
- python 并发编程 多线程 目录
线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...
- C++11 新特性之std::thread
C++11 新特性之std::thread 原文:https://blog.csdn.net/oyoung_2012/article/details/78958274 从C++11开始,C++标准库已 ...
- C++11新特性以及std::thread多线程编程
一 .C++11新特性 1. auto 类型推导 1.1 当=号右边的表达式是一个引用类型时,auto会把引用抛弃,直接推导出原始类型: 1.2 当=号右边的表达式带有const属性时,auto不会使 ...
最新文章
- android焦距跟实际距离,手机等效焦距一算便知_nubia Z5S Mini_手机Android频道-中关村在线...
- [201804012]关于hugepages 3.txt
- php面向对象分页,PHP基于面向对象封装的分页类示例
- cocurrent包semaphore信号量
- 一个模型能服务所有的点击率模型?
- MySQL面试准备——64页pdf
- java小程序开发平台,隔壁都馋哭了
- ArcGIS Server中各种服务详解
- 网易云 NeteaseCloudMusicApi 码云下载
- 模糊聚类及matlab实现,matlab模糊聚类程序
- 基于SIMULINK的AWGN信道仿真
- mysql连接timeout_mysql 连接超时wait_timeout问题解决
- OpenAtom XuperChain开发者夏季论坛落幕,多位行业大咖共话开源区块链前景
- idea gwt 怎么编译_带Spring Boot的GWT
- 单词学习-Unit8Text3-2(15年7月30日,第9天)
- 计算机临床医学自动化哪个好,这五个专业待遇高,就业好,但是很难学,挂科风险大,选择要慎重...
- Android 进阶笔记,包含常用的技术框架、博客社区、书籍等。
- JDK1.8新特性:lambda表达式
- 区块链运作机制_什么是区块链及其运作方式?
- MUR1100-ASEMI轴向快恢复二极管MUR1100
热门文章
- 计算机硬件系统的构成教学设计,2.1 计算机硬件系统教学设计思路
- 简述select语句的完整语法_SQL里的各种语句的完整语法
- java 中的流_Java中的流(IO
- angular路由传递参数_在angular4.0路由传递获取参数的最优方案
- windows和linux允许分片,请问hadoop的hdfs文件系统和本地windows文件系统或linux文件系统是什么关系啊,谢谢...
- 3dobject用什么打开_第一次用开塞露是什么感觉?网友:像打开了新世界的大门
- native层 安卓_安卓逆向——拼xx协议java层分析
- python数据类型所占字节数_python标准数据类型 Bytes
- 饿了么商家电脑版_饿了么企业版荣膺“2020中国十大影响力人力资源品牌”大奖...
- linux rsync删文件速度,为什么用rsync删除大量文件的时候比用rm快