转自:http://www.cnblogs.com/haippy/p/3236136.html

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

std::thread 在 <thread> 头文件中声明,因此使用 std::thread 时需要包含 <thread> 头文件。

std::thread 构造

default (1)
thread() noexcept;
initialization (2)
template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);
copy [deleted] (3)
thread (const thread&) = delete;
move (4)
thread (thread&& x) noexcept;
  • (1). 默认构造函数,创建一个空的 thread 执行对象。
  • (2). 初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
  • (3). 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
  • (4). move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。
  • 注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

std::thread 各种构造函数例子如下(参考):

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>void f1(int n)
{for (int i = 0; i < 5; ++i) {std::cout << "Thread " << n << " executing\n";std::this_thread::sleep_for(std::chrono::milliseconds(10));}
}void f2(int& n)
{for (int i = 0; i < 5; ++i) {std::cout << "Thread 2 executing\n";++n;std::this_thread::sleep_for(std::chrono::milliseconds(10));}
}int main()
{int n = 0;std::thread t1; // t1 is not a threadstd::thread t2(f1, n + 1); // pass by valuestd::thread t3(f2, std::ref(n)); // pass by referencestd::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a threadt2.join();t4.join();std::cout << "Final value of n is " << n << '\n';
}

move 赋值操作

move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;
  • (1). move 赋值操作,如果当前对象不可 joinable,需要传递一个右值引用(rhs)给 move 赋值操作;如果当前对象可被 joinable,则 terminate() 报错。
  • (2). 拷贝赋值操作被禁用,thread 对象不可被拷贝。

请看下面的例子:

#include <stdio.h>
#include <stdlib.h>#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for

void thread_task(int n) {std::this_thread::sleep_for(std::chrono::seconds(n));std::cout << "hello thread "<< std::this_thread::get_id()<< " paused " << n << " seconds" << std::endl;
}/** ===  FUNCTION  =========================================================*         Name:  main*  Description:  program entry routine.* ========================================================================*/
int main(int argc, const char *argv[])
{std::thread threads[5];std::cout << "Spawning 5 threads...\n";for (int i = 0; i < 5; i++) {threads[i] = std::thread(thread_task, i + 1);}std::cout << "Done spawning threads! Now wait for them to join\n";for (auto& t: threads) {t.join();}std::cout << "All threads joined.\n";return EXIT_SUCCESS;
}  /* ----------  end of function main  ---------- */

其他成员函数

  • get_id
获取线程 ID。
  • joinable
检查线程是否可被 join。
  • join
Join 线程。
  • detach
Detach 线程
  • swap
Swap 线程 。
  • native_handle
返回 native handle。
  • hardware_concurrency [static]
检测硬件并发特性。

std::thread详解相关推荐

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

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

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

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

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

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

  4. C++11 并发指南三(std::mutex 详解)

    上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...

  5. 【转】C++11 并发指南五(std::condition_variable 详解)

    http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...

  6. C++11 并发指南五(std::condition_variable 详解)

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

  7. c++ std::move详解

    c++ std::move详解 在移动构造中的时候,移动拷贝其实就是把原来参数对象的指针地址给到了构造的对象,再把原先对象的指针置为NULL,这样内存就不会被原来函数给析构了.对于实体的对象执行的其实 ...

  8. python 线程thread详解

    join详解 看到代码示例时,都会出现一个join,这个作用如何?先看结论 阻塞主程序,专注于执行多线程中的程序 多线程多join的情况下,依次执行各线程的join方法,子线程全部结束了才能执行主线程 ...

  9. std::mutex详解

    Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文件中,所以如果你需要使用 std::mutex,就必须包含 <mute ...

最新文章

  1. PHP Log时时查看小工具
  2. 常见HTTP状态码列表
  3. 吸烟致癌的迷思是如何破除的?
  4. 数据挖掘算法 1 ID3(python)
  5. 超燃动态可视化条形图源码及效果图_40行不到的Python代码实现超燃动态排序图...
  6. java 货币格式 转换_java格式化数值成货币格式示例
  7. python爬楼梯问题_使用python算法解决楼梯台阶问题方法详解
  8. Unity VR游戏教程
  9. 美元升值对中国资产价格的影响
  10. 如何破解私域留存?四大核心环节拆解锁客关键能力
  11. 数据治理(十一):数据安全管理Ranger初步认识
  12. pm2 for linux
  13. 多因子量化选股(1)
  14. asp.net mvc 连接sqlserver数据库
  15. 广州医保上线移动支付:暂只针对普通门诊
  16. Part 10:iOS的数据持久化(1),文件,归档
  17. tf 设置多显卡_让显卡再次危机,《孤岛危机》重置版能否找回当年的感动
  18. 路平石模具铺设路缘石公路项目质量提升的过程
  19. lol手游服务器维护到什么时候,lol10.1版本维护到几点 lol维护公告最新2020
  20. 用java编写连连看_用java语言编写连连看游戏

热门文章

  1. 华为云客户端_华为公布云手机计费清单,要不要光刻机也给出了答案
  2. 世界上最难的视觉图_世界上最长的蛇有多长?四川惊现55米洪荒巨蟒(图)
  3. c语言将数据写不入文件,求大神看看为什么不能将数据写入文件
  4. linux版_微软爱 Linux:安全杀毒软件 Defender ATP 要出 Linux 版了! | Linux 中国
  5. postmapping注解参数说明_从零搭建后端框架:优雅的参数校验Validator
  6. cnn生成图像显著图_基于CNN与图像前背景分离的显著目标检测
  7. _linux中curl命令详解-linux运维
  8. vb.net中递归退到最外层_数组中的逆序对
  9. main方法 如何去掉http debug日志_在MyBatis中如何使用collection标签实现嵌套查询?...
  10. 加载文件流_jvm类加载的过程