原标题:C++11并发编程:多线程std:thread

一:概述

C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改。现在在C++11中只需使用语言层面的thread可以解决这个问题。

所需头文件

二:构造函数

1.默认构造函数

thread() noexcept

一个空的std::thread执行对象

2.初始化构造函数

template

explicit thread(Fn&& fn, Args&&... args);

创建std::thread执行对象,线程调用threadFun函数,函数参数为args。

void threadFun(int a)

{

cout << "this is thread fun !" << endl;

}

thread t1(threadFun, 2);

3.拷贝构造函数

thread(const thread&) = delete;

拷贝构造函数被禁用,std::thread对象不可拷贝构造

void threadFun(int& a)

{

cout << "this is thread fun !" << endl;

}

int value = 2;

thread t1(threadFun, std::ref(value));

4.Move构造函数

thread(thread&& x)noexcept

调用成功原来x不再是std::thread对象

void threadFun(int& a)

{

cout << "this is thread fun !" << endl;

}

int value = 2;

thread t1(threadFun, std::ref(value));

thread t2(std::move(t1));

t2.join();

三:成员函数

1.get_id()

获取线程ID,返回类型std::thread::id对象。

thread t1(threadFun);

thread::id threadId = t1.get_id();

cout << "线程ID:" << threadId << endl;

//threadId转换成整形值,所需头文件

ostringstream oss;

oss << t1.get_id();

string strId = oss.str();

unsigned long long tid = stoull(strId);

cout << "线程ID:" << tid << endl;

2.join()

创建线程执行线程函数,调用该函数会阻塞当前线程,直到线程执行完join才返回。

thread t1(threadFun);

t1.join() //阻塞等待

3.detach()

detach调用之后,目标线程就成为了守护线程,驻留后台运行,与之关联的std::thread对象失去对目标线程的关联,无法再通过std::thread对象取得该线程的控制权。

4.swap()

交换两个线程对象

thread t1(threadFun1);

thread t2(threadFun2);

cout << "线程1的ID:" << t1.get_id() << endl;

cout << "线程2的ID:" << t2.get_id() << endl;

t1.swap(t2);

cout << "线程1的ID:" << t1.get_id() << endl;

cout << "线程2的ID:" << t2.get_id() << endl;

5.hardware_concurrency()

获得逻辑处理器储量,返回值为int型

int coreNum = thread::hardware_concurrency();

四:使用

1.创建线程

void threadFun1()

{

cout << "this is thread fun1 !" << endl;

}

int main()

{

thread t1(threadFun1);

t1.join();

getchar();

return 1;

}

2.创建线程,传参

void threadFun1(int v)

{

cout << "this is thread fun1 !" << endl;

cout << v << endl;

}

int main()

{

int value = 6;

thread t1(threadFun1, value);

t1.join();

getchar();

return 1;

}

需要注意,变量int value 和int v 做变量传递时并不是引用,而是对变量做了拷贝,所以在传递给int v前,int value不能出作用域(释放了内存),join(),可以保证int value变量释放内存,如果使用detach(),可能存在这种情况。

3.创建线程,引用传参

void threadFun1(int& v)

{

cout << "this is thread fun1 !" << endl;

cout << v << endl;

}

int main()

{

int value = 6;

thread t1(threadFun1, std::ref(value));

t1.join();

getchar();

return 1;

}

4.创建建线程,线程函数为类成员函数

class Object

{

public:

Object()

{

cout << "构造函数" << endl;

}

~Object()

{

cout << "析构函数" << endl;

}

void fun(string info)

{

cout << info << endl;

}

};

int main()

{

Object obj;

string str = "我是一个类的成员函数!";

thread t1(&Object::fun, &obj, str);

t1.join();

getchar();

return 1;

---------------------

作者:蜗牛201

原文:https://blog.csdn.net/woniu211111/article/details/85123281

责任编辑:

c+++11并发编程语言,C++11并发编程:多线程std:thread相关推荐

  1. C++11并发编程:多线程std::thread

    一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...

  2. C++11 多线程(std::thread)详解

    注:此教程以 Visual Studio 2019 Version 16.10.3 (MSVC 19.29.30038.1) 为标准,大多数内容参照cplusplus.com里的解释 此文章允许转载, ...

  3. C++多线程编程实战01:std::thread

    C++多线程:std::thread 文章目录 C++多线程:std::thread 定义 构造函数 析构函数 赋值操作函数 join与datch 例子 例子 其它 基本用法 线程参数 等待线程完成( ...

  4. C++11 并发指南四(future 详解一 std::promise 介绍)

    前面两讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread 和 std::m ...

  5. 【C/C++开发】C++11 并发指南二(std::thread 详解)

    上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...

  6. C++11 并发指南四(future 详解三 std::future std::shared_future)

    上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...

  7. C++11 并发指南四(future 详解二 std::packaged_task 介绍)

    上一讲<C++11 并发指南四(<future> 详解一 std::promise 介绍)>主要介绍了 <future> 头文件中的 std::promise 类, ...

  8. C++11 并发指南二(std::thread 详解)

    上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...

  9. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

最新文章

  1. ASIC开发设计流程
  2. (0060)iOS开发之iOS 9: UIStackView入门
  3. linux正则表达式_号称“天书”的正则表达式,要这么来讲,我小学三年级已经满分了
  4. 需求分析师的工作重点
  5. 人脸检测与识别年度进展概述
  6. 电脑技巧:分享七个解决烦人的弹窗广告的小技巧
  7. leetcode20. 有效的括号
  8. minicom 串口信息过长分行显示
  9. ArcGIS 代理产品价格以及折扣表、产品描述
  10. 645. 错误的集合
  11. (三)线程同步工具集_1---控制线程并发访问一个资源
  12. 遥感动态监测实验(以福州为例)
  13. sql server2016导入excel错误
  14. Unity粒子特效的缩放与加(减)速
  15. rabbitmq启动报错,TCP connection succeeded but Erlang distribution failed
  16. 计算机 分类号,中图法分类号(计算机,自动化)(CLC number (computer, automation)).doc...
  17. 怎么转换视频格式,视频格式不符如何转换?
  18. 【SpringBoot】Error creating bean with name ‘methodValidationPostProcessor‘ defined in class path reso
  19. ROS2 基础概念 参数
  20. 市场调研-全球与中国沉鱼饲料市场现状及未来发展趋势

热门文章

  1. Android Studio-------添加按钮
  2. 2014 网选 5024 Wang Xifeng's Little Plot
  3. SD卡移植FAT32文件系统无MBR
  4. oracle11g ogg报价,Oracle11g GoldenGate配置错误OGG-00868 Attaching to ASM server
  5. gddr6速率_Rambus展示18GT/s的GDDR6内存子系统:高频信号纯净度仍然非常好
  6. java存储键值结构_java-键值存储为主数据库
  7. 华立学院计算机组成原理考试,广东工业大学华立学院计算机组成原理期末复习重点...
  8. 扫地机器人返充原理_扫地机器人全解析
  9. python数据预处理 重复行统计_Python数据分析之数据预处理(数据清洗、数据合并、数据重塑、数据转换)学习笔记...
  10. 谷歌浏览器中文版_中国科学家设计超薄指尖传感器,厚度不到A4纸五分之一 / 谷歌发布地图时光机:百年前,你家街道啥样?/ AI看图说话首超人类...