C++11 并发指南二(std::thread 详解)
上一篇博客《C++11 并发指南一(C++11 多线程初探)》中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用法。
std::thread 在 <thread> 头文件中声明,因此使用 std::thread 时需要包含 <thread> 头文件。
std::thread 构造
default (1) |
|
---|---|
initialization (2) |
|
copy [deleted] (3) |
|
move (4) |
|
- (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 = ; i < ; ++i) {
- std::cout << "Thread " << n << " executing\n";
- std::this_thread::sleep_for(std::chrono::milliseconds());
- }
- }
- void f2(int& n)
- {
- for (int i = ; i < ; ++i) {
- std::cout << "Thread 2 executing\n";
- ++n;
- std::this_thread::sleep_for(std::chrono::milliseconds());
- }
- }
- int main()
- {
- int n = ;
- std::thread t1; // t1 is not a thread
- std::thread t2(f1, n + ); // 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) |
|
---|---|
copy [deleted] (2) |
|
- (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[];
- std::cout << "Spawning 5 threads...\n";
- for (int i = ; i < ; i++) {
- threads[i] = std::thread(thread_task, i + );
- }
- 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++11 并发指南二(std::thread 详解)的更多相关文章
- C++11 并发指南二(std::thread 详解)(转)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- 【C/C++开发】C++11 并发指南二(std::thread 详解)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- 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/C++开发】C++11 并发指南三(std::mutex 详解)
本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )
C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍) 一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...
随机推荐
- docker 部署 java 项目
Docker Docker官方网址: https://docs.docker.com/ 英文地址 Docker中文网址: http://www.docker.org.cn/ 中文地址 Docker是 ...
- css清除默认样式
CSS 清除默认样式 通常有以下几句就够了: *{margin:0;padding:0} li{list-style:none} img{vertical-align:top;border:non ...
- htmlElement.style 是只读属性
document.getElementById('test').style = 'opacity:0'; 在某些机型上,比如苹果 ios 10机型上,会报错.提示 style 属性为只读属性. 建议所 ...
- java enum使用方法
直接上手吧,注释都写清楚了 编写枚举类 /** * 可以使用接口或类包裹枚举元素,使其可以统一调用入口 */ public interface TestEnumIntfc { /** * 创建枚举对象 ...
- git本地仓库关联多个remote,怎么用本地一个分支向不同remote不同分支推送代码
我想这个问题,是大家关注的问题,这个问题,我非常关注. 背景:在公司开发项目,我们一般都要把项目推送到公司领导创建的一个远程仓库里边去,但是我们同时也有自己的小仓库,这样的话,如何方便的将我们的代码, ...
- python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义
本文内容: 正则表达式常用字符. 常用可选标志位. group与groups. match. search. sub. split findall. compile 特殊字符转义 一些现实例子 首发时 ...
- JS笔记(一):基础知识
(一) 标识符 标识符就是一个名字,在JS中,标识符用来对变量和函数命名,或者用做JS代码中某些循环语句中的跳转位置的标记.JS的标识符必须以字母._或$符号开始,后续字符可以是字母.数字._或$符号 ...
- plsql备份表---只是表---不包含表数据
写这个的同时还在备份,表的数据进度很慢,数据太大了. 用的工具是plsql 导出表:点击 tool工具 ---> export user object 导出用户目标 ----> ...
- Unity Chan 3D Asset
Unity Chan 3D Asset 我真的很久沒再家裡開unity,不過今天让我久违的開了 下载地址 :http://ref.gamer.com.tw/redir.php?url=http%3A ...
- 实战:阿里巴巴 DevOps 转型后的运维平台建设
导读:阿里巴巴DevOps转型之后,运维平台是如何建设的?阿里巴巴高级技术专家陈喻结合运维自身的理解,业务场景的分析和业界方法论的一些思考,得出来一些最佳实践分享给大家. 前言 “我是这个应用 ...