上一篇博客《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]
检测硬件并发特性。

转载于:https://www.cnblogs.com/huty/p/8516998.html

【C/C++开发】C++11 并发指南二(std::thread 详解)相关推荐

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

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

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

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

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

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

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

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

  5. C++11 并发指南六( atomic 类型详解二 std::atomic )

    C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)  一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...

  6. C++11 并发指南六(atomic 类型详解四 C 风格原子操作介绍)

    前面三篇文章<C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)>.<C++11 并发指南六( <atomic> 类型详解二 std::at ...

  7. C++11 并发指南六(atomic 类型详解三 std::atomic (续))

    C++11 并发指南六( <atomic> 类型详解二 std::atomic ) 介绍了基本的原子类型 std::atomic 的用法,本节我会给大家介绍C++11 标准库中的 std: ...

  8. C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)

    C++11 并发指南已经写了 5 章,前五章重点介绍了多线程编程方面的内容,但大部分内容只涉及多线程.互斥量.条件变量和异步编程相关的 API,C++11 程序员完全可以不必知道这些 API 在底层是 ...

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

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

最新文章

  1. Charles 河畔的大圆顶
  2. 学会理解并编辑/etc/fstab
  3. [导入]韩语基本会话
  4. 优化 Tengine HTTPS 握手时间
  5. builtins.TypeError: sequence item 0: expected a bytes-like object, str found
  6. C#坏习惯:通过不好的例子学习如何制作好的代码——第1部分
  7. HTML前端开发之路——Transition
  8. SVM支持向量机:分类、回归和核函数
  9. LeetCode IPO
  10. ue4服务器稳定性,ue4 客户端 服务器
  11. JSP 页面传值方法总结(转)
  12. 碧桂园建筑机器人造楼,梦照进现实还是“海市蜃楼”?
  13. FbinstTool最简单制作U盘启动ISO格式(金测)
  14. 【AR开发】ARCore简介
  15. 带瀑布流的电钢琴_你没有看错 这个Find智能钢琴它会自己弹琴
  16. 提升30%转化率的LBS网络营销神器--高精准IP定位
  17. saas智能营销云平台是什么 大数据营销智能平台 - whale 帷幄
  18. SDOI 2018二轮题解(除Day2T1)
  19. 百度提交死链的官方标准格式
  20. 软件 黑苹果盒盖不休眠_怎么解决苹果电脑合盖自动休眠问题?

热门文章

  1. 【计蒜客 - 2019南昌邀请赛网络赛 - M】Subsequence(字典树,dp预处理)
  2. 【CodeForces - 438D】The Child and Sequence(线段树区间取模操作)
  3. 【Hihocoder - 1723】子树统计(线性基合并)
  4. 百度顶会论文复现(4):飞桨API详解
  5. 计划的主体部分应有哪些内容_本科论文查重查哪些部分内容?需要注意什么?...
  6. python实现k均值算法_python实现kMeans算法
  7. c#解决在数据表格中无法显示秒数问题
  8. Java在Linux下创建文件,文件夹,删除,拷贝文件的命令
  9. CRegKey 注册表操作
  10. Python(21)--变量进阶