【C/C++开发】C++11 并发指南二(std::thread 详解)
上一篇博客《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> |
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 thread
std::thread t2(f1, n + 1); // pass by value
std::thread t3(f2, std::ref(n)); // pass by reference
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.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 ---------- */

其他成员函数
- 获取线程 ID。
- 检查线程是否可被 join。
- Join 线程。
- Detach 线程
- Swap 线程 。
- 返回 native handle。
- 检测硬件并发特性。
【C/C++开发】C++11 并发指南二(std::thread 详解)的更多相关文章
- 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/C++开发】C++11 并发指南三(std::mutex 详解)
本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...
- 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 并发指南五(std::condition_variable 详解)(转)
前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...
- C++11 并发指南三(std::mutex 详解)(转)
转自:http://www.cnblogs.com/haippy/p/3237213.html 上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::th ...
- 【转】C++11 并发指南五(std::condition_variable 详解)
http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )
C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍) 一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...
随机推荐
- 北理工机器人队RM视觉组学习参考汇总(持续更新中)
欢迎大家有意加入北理工机器人队参与到视觉组的工作中.在大家能够正式作为队员参与到视觉组的准备工作之前,北理机器人队需要对各位进行培训.这篇文章主要面向有志于参加机器人队视觉组的同学.同时,欢迎所有对相 ...
- 从零开始一步一步搭建Ubuntu Server服务器、修改数据源、安装Docker、配置镜像加速器、Compose部署Gitlab服务
场景 最终目的是使用Docker Compose部署一个Gitlab服务. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序 ...
- kindedit,uedit 上传跨域返回
1.kindedit 跨域上传图片的时候,a.com 上传到b.com接收图片服务器,然后返回图片地址. 2.一般如果不做任何处理是获取不到返回的信息的.原因是跨域了 3.所以一般在上传成功后,在跳转 ...
- 【开发笔记】- 输出String字符串使其文本对齐
需求 一段文本做每64个字节换行处理,并添加对应的头尾注释 代码实现 public static String certFormat(String code, String beginTitle, S ...
- elementui 自定义表头 renderHeader的写法 给增加el-tooltip的提示
1.html <el-table-column prop="taxes" :render-header="renderHeader" width=&quo ...
- UUID生成库libuuid和crossguid
libuuid是一个开源的用于生成UUID(Universally Unique Identifier,通用唯一标识符)的库. 可从https://sourceforge.net/projects/l ...
- EntityManager 的复杂查询
EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满足条件的实体Bean.实体B ...
- angular6 导出Excel文件
1.安装file-saver.@types/file-saver和xlsx npm install file-saver --save npm install @types/file-saver -- ...
- Linux 批量查找并替换文件夹下所有文件的内容
1.批量查找某个目下文件的包含的内容 cd etc grep -rn "查找的内容" ./ 2.批量替换某个目下所有包含的文件的内容 cd etc sed -i "s/查 ...
- Python在Linux环境中安装Thrift
1.文件下载:thrift-0.11.0.tar.gz 个人网盘下载:链接:https://pan.baidu.com/s/1MXgx8LuN4wk7ssVUD9Wzaw 提取码:xw85 2. ...