[原]C++新标准之std::thread
概览
从C++11开始提供了线程的支持,终于可以方便的编写跨平台的线程代码了。除了std::thread类,还提供了许多其它便利同步的机制,本篇总结是C++11学习笔记系列的首篇总结。
std::thread
std::thread定义在<thread>中,提供了方便的创建线程的功能。
类定义
- class thread
- {
- public:
- thread() noexcept;
- thread( thread&& other ) noexcept;
- template< class Function, class... Args >
- explicit thread( Function&& f, Args&&... args );
- thread(const thread&) = delete;
- ~thread();
- thread& operator=( thread&& other ) noexcept;
- bool joinable() const noexcept;
- std::thread::id get_id() const noexcept;
- native_handle_type native_handle();
- void join();
- void detach();
- void swap( thread& other ) noexcept;
- static unsigned int hardware_concurrency() noexcept;
- };
从定义中我们可以得知:
- std::thread不支持拷贝语义。
- std::thread支持移动语义。
各个成员函数的简单介绍
- join() 可以用来等待线程结束,只能调用一次。
- joinable()是否与某个有效的线程关联。
- detach() 与当前线程分离。
- swap() 与另外一个std::thread交换。
- get_id()获取id。
- native_handle() 返回平台相关的数据,windows下是HANDLE。
- hardware_concurrency() 返回可并行运行的线程数量,只能作为一个参考。
例子
因为thread类比较简单,我们通过几个例子来学习。
- 支持移动语义,但不支持拷贝语义
- #include <thread>
- void some_function() {}
- void some_other_function() {}
- int main()
- {
- std::thread t1(some_function); // 构造一个thread对象t1
- std::thread t2 = std::move(t1); // 把t1 move给另外一个thread对象t2,t1不再管理之前的线程了。
- // 这句不需要std::move(),从临时变量进行移动是自动和隐式的。调用的是operator=(std::thread&&)
- t1 = std::thread(some_other_function);
- std::thread t3;
- t3 = std::move(t2); // 把t2 move给t3
- // 把t3 move给t1,非法。因为`t1`已经有了一个相关的线程,会调用`std::terminate()`来终止程序。
- t1 = std::move(t3);
- }
- 通过调用join()成员函数来等待线程结束
- #include <iostream>
- #include <thread>
- #include <chrono>
- void foo()
- {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- int main()
- {
- std::cout << "starting first helper...\n";
- std::thread helper1(foo);
- helper1.join();
- }
- 传递参数给线程函数
- void f(int i, std::string const& s);
- std::thread t(f, 3 "hello");
注意:参数会以默认的方式被复制到内部存储空间,直到使用的时候才会转成对应的类型。
下面的例子有问题吗?有什么问题?
- void f(int i, std::string const& s);
- void oops(int some_param)
- {
- char buffer[1024];
- sprintf(buffer, "%i", some_param);
- std::thread t(f, 3, buffer);
- t.detach();
- }
局部变量buffer的指针会被传递给新线程,如果oops()在buffer被转换成string之前退出,那么会导致未定义的行为。解决之道是在构造std::thread的时候传递string变量。std::thread t(f, 3, std::string(buffer));
可以使用std::ref()来显示表明要传递引用,就像std::bind()那样。
- std::thread t(update_data_for_widget, w, std::ref(data));
- 使用类的成员函数作为线程参数
- #include <thread>
- #include <string>
- class CRunner
- {
- public:
- void run0(){}
- void run1(int a) {}
- void run2(int a, int b) const {}
- int run3(int a, char b, const std::string& c) {return 0;}
- int run4(int& a, double b, float c, char d) { ++a; return 0; }
- static void run_static(int a) {}
- };
- int main()
- {
- CRunner runner;
- int a = 0;
- // 使用std::mem_fun,需要传指针
- std::thread t0(std::mem_fun(&CRunner::run0), &runner);
- // 使用std::mem_fun_ref,可以传引用或副本
- std::thread t1(std::mem_fun_ref(&CRunner::run1), std::ref(runner), 1);
- // std::thread t1(std::mem_fun_ref(&CRunner::run1), runner, 1);
- // 使用std::mem_fn,std::mem_fn支持多于一个参数的函数,std::mem_fun不支持。
- std::thread t2(std::mem_fn(&CRunner::run2), std::ref(runner), 1, 2);
- // 使用std::bind + std::mem_fn
- std::thread t3(std::bind(std::mem_fn(&CRunner::run3), &runner, 1, 2, "data"));
- // 使用std::mem_fn,注意std::ref的用法,如果不用std::ref行不行?
- std::thread t4(std::mem_fn(&CRunner::run4), &runner, std::ref(a), 2.2, 3.3f, 'd');
- // 使用类的静态函数
- std::thread t5(&CRunner::run_static, 1);
- t0.join();
- t1.join();
- t2.join();
- t3.join();
- t4.join();
- t5.join();
- }
注:
- std::mem_fun需要与类指针配合,std::mem_fun_ref可以和类引用或类副本配合。
- std::mem_fun或std::mem_fun_ref只支持最多一个参数的可调用对象(函数,仿函数等),在新标准中已经废弃不用了。
- std::mem_fn是std::mem_fun的增强版,不需要区分传递指针或者传递引用。而且可以支持传递多个参数。
- std::thread只需要一个可以调用的对象(函数,仿函数等),所以我们也可以通过std::bind()的返回值来作为std::thread的参数。
更多
虽然在之前的例子中的函数有返回值,但是我们却不能获得,想获得返回值我们需要使用std::future,关于std::future的总结,后续会慢慢补充,敬请期待。
参考资料
- 《C++并发编程实战》
- cppreference
[原]C++新标准之std::thread的更多相关文章
- [原]C++新标准之std::chrono::duration
原 总结 C++11 chrono duration ratio 概览 std::chrono::duration 描述 类定义 duration_cast()分析 预定义的duration 示例代 ...
- [原]C++新标准之std::chrono::time_point
原 总结 STL 标准库 chrono time_point ratio 概览 类定义 总结 思考 拓展 system_clock steady_clock high_resolution_cloc ...
- [原]C++新标准之std::ratio
原 总结 ratio 概览 类定义 预定义ratio 应用 示例代码 参考资料 概览 std::ratio定义在<ratio>文件中,提供了编译期的比例计算功能.为std::chrono ...
- mingw-w64线程模型:posix vs win32(posix允许使用c++11的std:: thread,但要带一个winpthreads,可能需要额外dll)
我正在安装 mingw-w64 on Windows,有两个选项: win32线程和posix线程. 我知道win32线程和pthreads之间的区别,但是我不明白这两个选项之间的区别. 我怀疑如果我 ...
- C++ std::thread概念介绍
C++ 11新标准中,正式的为该语言引入了多线程概念.新标准提供了一个线程库thread,通过创建一个thread对象来管理C++程序中的多线程. 本文简单聊一下C++多线程相关的一些概念及threa ...
- 第25课 std::thread对象的析构
一. 线程的等待与分离 (一)join和detach函数 1. 线程等待:join() (1)等待子线程结束,调用线程处于阻塞模式. (2)join()执行完成之后,底层线程id被设置为0,即join ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- std::thread使用
本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...
- C++11并发——多线程std::thread (一)
https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...
随机推荐
- maven 依赖报错
1 maven项目,在Intellij 右侧 Maven projects - Lifecycle - clean , validate, compile, ….,右击clean,选中Run ‘pro ...
- js 月份选择器(只选择到月)
需要如下js https://pan.baidu.com/s/1c1T9wY0 在html中添加如下代码 <input onclick="setmonth(this)" /& ...
- Docker部署NETCORE应用程序
Docker部署NETCORE应用程序 创建.netcore项目,本文以ASP.NET Core2.2 版本为例 设置Dockerfile属性始终复制 编辑Dockerfile内容 FROM micr ...
- 完美的代价(swap成回文串、贪心)
Description 回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的.小龙龙认为回文串才是完美的. 现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串 ...
- Essay写作的六大黄金法则以及四大禁区
虽然Essay这么难写,但是,也有一些可以拿高分的准则,本文小编就为大家分享高分Essay写作必知黄金法则,希望对想要在Essay拿高分的留学生小伙伴们有些帮助. 黄金法则1.关注相关问题的重点词汇 ...
- 关于Wasserstein GAN的一些笔记
这篇笔记基于上一篇<关于GAN的一些笔记>. 1 GAN的缺陷 由于 $P_G$ 和 $P_{data}$ 它们实际上是 high-dim space 中的 low-dim manifol ...
- MySQL-半同步复制原理实践
参考文档: http://mysql.taobao.org/monthly/2017/04/01/ 阿里内核月报半同步复制的数据一致性 https://www.cnblogs.com/ivictor/ ...
- PAT Advanced 1003 Emergency (25) [Dijkstra算法]
题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...
- Day 5 :ArrayList原理、LinkedList原理和方法和迭代器注意事项
迭代器在变量元素的时候要注意事项: 在迭代器迭代元素 的过程中,不允许使用集合对象改变集合中的元素个数,如果需要添加或者删除只能使用迭代器的方法进行操作. 如果使用过了集合对象改变集合中元素个数那 ...
- JZOJ-2019-11-5 A组
T1 给定由 n 个点 m 条边组成的无向连通图,保证没有重边和自环. 你需要找出所有边,满足这些边恰好存在于一个简单环中.一个环被称为简单环,当且仅当它包含的所有点都只在这个环中被经过了一次.(即求 ...