文章目录

  • 1. 无参
  • 2. 参数传入
  • 3. 输入输出参数
  • 4. lambda表达式
  • 5. 类的成员函数

1. 无参

#include <iostream>
#include <thread>void hello()
{std::cout << "Hello Concurrent, thread id: " << std::this_thread::get_id() << std::endl;
}int main()
{std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;std::thread t(hello);t.join();return 0;
}

运行结果:

  • 包含头文件 #include <thread> ;
  • 创建 std::thread 的实例,传入hello函数,子线程执行hello函数;
  • join 等待子线程完成。 或使用 detach 分离子线程与主线程,使主线程无需等待子线程完成。

2. 参数传入

#include <iostream>
#include <thread>
#include <string>void hello_input(std::string str)
{std::cout << str << ", hello Concurrent, thread id: " << std::this_thread::get_id() << std::endl;
}int main()
{std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;std::thread t2(hello_input, "test");t2.join();return 0;
}

运行结果:

参数作为 std::thread 对象的第二个参数传入

3. 输入输出参数

#include <iostream>
#include <thread>
#include <string>void hello_input_output(int a, int b, int& ret)
{ret = a + b;std::cout << "a + b: " << ret << ", hello Concurrent, thread id: " << std::this_thread::get_id() << std::endl;
}int main()
{std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;int ret = 0;std::thread t3(hello_input_output, 1, 5, std::ref(ret));t3.join();std::cout << "hello_input_output, ret: " << ret << std::endl;return 0;
}

运行结果:

这里是使用参数引用来作为输出参数, 使用的时候, std::thread 对象中传入的输出参数需要使用 std::ref() 函数加以包装。

线程库的内部代码会把参数的副本当成move-only型别(只能移动,不可复制),这些副本被当成临时变量,以右值形式传给新线程上的函数。最终,hello_input_output() 函数调用会收到一个右值作为参数。如果不加 std::ref() 这段代码会编译失败。

std::ref() 后, hello_input_output() 函数收到的是ret变量的引用,而不是临时副本。

4. lambda表达式

#include <iostream>
#include <thread>int main()
{std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;std::thread t4([] {std::cout << "lambda, thread id: " << std::this_thread::get_id() << std::endl;});t4.join();return 0;
}

运行结果:

5. 类的成员函数

#include <iostream>
#include <thread>
#include <string>class MyClass
{public:void do_some_thing(std::string str) {m_strstr = str;std::cout << this << "  " << str << ", class member function, thread id: " << std::this_thread::get_id() << std::endl;};std::string getStrr() const { return m_strstr; };private:std::string m_strstr;};int main()
{std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;MyClass myClass;std::thread t5(&MyClass::do_some_thing, &myClass, "test");t5.join();std::cout << &myClass << "  " << "myclass.getStrr: " << myClass.getStrr() << std::endl;return 0;
}

运行结果:

若要将某个类的成员函数设定为线程函数:

  • 第一个参数,传入一个函数指针,指向该成员函数;
  • 第二个参数,对象指针&myClass,或者std::ref(myClass)。 如果写成myClass,不带&或者std::ref,则创建线程的过程中,myClass被拷贝了一份;
  • 第三个及以后的参数,为调用的成员函数的参数。

C++并发 std::thread相关推荐

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

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

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

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

  3. c+++11并发编程语言,C++11并发编程:多线程std:thread

    原标题:C++11并发编程:多线程std:thread 一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植, ...

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

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

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

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

  6. 并发,std::thread

    2019独角兽企业重金招聘Python工程师标准>>> std::thread  定义一个线程对象,并管理关联的线程 备注: 你可以使用 thread 对象来观察和管理在应用程序中的 ...

  7. 【多线程】C++11进行多线程开发 (std::thread)

    文章目录 创建线程 std::thread 类 使用join() 使用 detach() 警惕作用域 线程不能复制 给线程传参 传递指针 传递引用 以类成员函数为线程函数 以容器存放线程对象 互斥量 ...

  8. C++11学习笔记-----线程库std::thread

    在以前,要想在C++程序中使用线程,需要调用操作系统提供的线程库,比如linux下的<pthread.h>.但毕竟是底层的C函数库,没有什么抽象封装可言,仅仅透露着一种简单,暴力美 C++ ...

  9. C++ std::thread 和 std::jthread 使用详解 (含C++20新特性)

    目录 std::thread std::thread 构造函数 观察器 操作 std::jthread std::jthread 构造函数 观察器 操作 停止记号处理 管理当前线程的函数 yield( ...

  10. C++11并发之std::thread

    C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic 本文概要: 1.成员类型和成员函数. 2.std::thread ...

最新文章

  1. NB-IoT与LoRa,两马领跑物联网竞赛,谁将最终胜出?
  2. Redis 学习之事务处理
  3. WebRTC之STUN、TURN和ICE研究
  4. 移动技术--从网页游戏谈起 1
  5. LogViewer-Unity日志的插件
  6. android授权新浪微博 服务端,android应用关于新浪微博授权登陆解决方案
  7. 幻影机器人庄园参观路线_上海幻影机器人庄园攻略,上海幻影机器人庄园门票/游玩攻略/地址/图片/门票价格【携程攻略】...
  8. 解密Zynga:专注 流水线 数据控
  9. CryEngine 渲染流程
  10. iTest使用说明_V4.5.1
  11. 【解决方案】雷电模拟器去桌面底部游戏和游戏中心
  12. git commit 约定式提交
  13. 项目管理文档_PPM优课第18期 | 不同项目管理模式“武装”质量管理体系的搭建...
  14. Python 解析har 文件将域名分类导出
  15. 80后上什么大学重要吗?看看这些IT富豪吧
  16. Bug管理工具Bugtags——初认知
  17. 除了ChatGPT,这20款AI神器同样值得你使用
  18. 3.1 Python图像的频域图像增强-图像的傅里叶变换和反变换
  19. 关于expdp ESTIMATE_only以及EXPDP和EXP HWM降低的比较
  20. 工业互联网 第 4 章 各类工业互联网平台介绍以及OneNET平台使用

热门文章

  1. PHP运行Python脚本
  2. 没有巨头公司做内容支撑 剪辑软件公司小影科技上市后能跑多远?
  3. 入门计算机的粗略学习-Day13
  4. Error: Network Error
  5. 【GTK】【linux】如何使用gtk-demo里面的例程
  6. 【电商运营】试试这5种个性化营销方法,告别无效营销!
  7. TPO Official 01 Speaking
  8. 安装wincap时报错解决方式: an error occured when installing winpcap 0x00000430
  9. 计算机鼠标左键不起作用,电脑鼠标左键突然不好用了 右键没问题
  10. beyong compare激活