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。
- 检测硬件并发特性。
- 转自:http://www.cnblogs.com/haippy/p/3236136.html
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 ...
随机推荐
- python-高级编程-04
[http协议] 断句 : 由于tcp协议是基于流的传输协议,也就是在传输层本身是做不到断句的功能的, 于是断句需要在应用层协议实现. 最初用回车和换行来标示一套命令的结束 如果信息里面有 \r\n ...
- PostgreSQL 全文索引
-- 首先要创建自定义的词典,在不使用停用词文件的情况下创建自定义词典,例如: CREATE TEXT SEARCH DICTIONARY english_stem_nostop ( Template ...
- J2ee项目 编译依赖顺序
这儿有个帖子, 最后一个回复是: “我把我项目的libraries的"Order and Export"中的JRE与J2EE顺序换了一个问题解决”. 帖子地址: http://b ...
- HDU——1846Brave Game(巴什博弈)
Brave Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- DataSet中的表动态设置主键外键的方法
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] protected void pk_Click(object sender, EventArgs e) { ...
- 【Visual Studio】VS2013的Release模式下进行调试(转)
原文转自 http://blog.csdn.net/haizimin/article/details/50262901 在有的情况下,我们可能不能直接利用Debug模式进行程序调试,那么如何在Rele ...
- 转 整理 Linux服务器部署系列之一—Apache篇2
http://www.jb51.net/article/46148.htm 如何查看Apache的连接数和当前连接数 查看了连接数和当前的连接数 netstat -ant | grep $ip:80 ...
- mysql 初始化脚本
脚本须知: 1. 确认mysql的数据目录,二进制日志目录,中继日志的目录,安装目录的位置 2. 初始化会对前三个目录执行清空操作,不过清空前该脚本对其进行了压缩打包统一存放在/tmp目录下 3. 脚 ...
- poj 1950(搜索)
Dessert Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5430 Accepted: 2029 Descripti ...
- AC日记——Dylans loves tree hdu 5274
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...