c++11: <thread>学习
<thread>头文件中包含thread类与this_thread命名空间,下面逐一介绍。
thread类
1. 构造函数
(1)默认构造函数
thread() noexcept;
默认构造函数不执行任何线程,产生线程对象的线程ID为0。
(2)初始化构造函数
template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);
产生一个thread对象,提供一个joinable的线程并开始执行。joinable的线程在它们被销毁前需要被join或detach(否则线程资源不会被完全释放)。
参数介绍:
fn: 指向函数的指针,指向成员函数的指针,或者任何有移动构造函数(?)的对象(例如重载了operator()的类对象,闭包和函数对象)。如果有返回值的话,返回值被丢弃。
arg…: 传递给fn的参数。它们的类型必须是可移动构造的(?)。如果fn是成员函数指针的话,第一个参数必须是调用此方法的对象(或者引用/指针)。
(3)拷贝构造函数
thread (const thread&) = delete;
删除拷贝构造函数(线程对象不能被拷贝)。
(4)移动构造函数
thread (thread&& x) noexcept;
如果x是一个线程的话,生成一个新的线程对象。这个操作不影响原线程运行,只是将线程的拥有者从x变为新对象,此时x不再代表任何线程(移交了控制权)。
参数介绍:
x: 要移动给构造构造函数的另一线程对象。
例子
// constructing threads
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector std::atomic<int> global_counter (0); void increase_global (int n) { for (int i=0; i<n; ++i) ++global_counter; } void increase_reference (std::atomic<int>& variable, int n) { for (int i=0; i<n; ++i) ++variable; } struct C : std::atomic<int> {
C() : std::atomic<int>(0) {}
void increase_member (int n) { for (int i=0; i<n; ++i) fetch_add(1); }
}; int main ()
{
std::vector<std::thread> threads; std::cout << "increase global counter with 10 threads...\n";
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(increase_global,1000)); std::cout << "increase counter (foo) with 10 threads using reference...\n";
std::atomic<int> foo(0);
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(increase_reference,std::ref(foo),1000)); std::cout << "increase counter (bar) with 10 threads using member...\n";
C bar;
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(&C::increase_member,std::ref(bar),1000)); std::cout << "synchronizing all threads...\n";
for (auto& th : threads) th.join(); std::cout << "global_counter: " << global_counter << '\n';
std::cout << "foo: " << foo << '\n';
std::cout << "bar: " << bar << '\n'; return 0;
}
显示结果
increase global counter using 10 threads...
increase counter (foo) with 10 threads using reference...
increase counter (bar) with 10 threads using member...
synchronizing all threads...
global_counter: 10000
foo: 10000
bar: 10000
2. 析构函数
~thread();
如果线程是joinable的,在销毁时会调用terminate();
3. 成员函数
(1)void detach();
使该线程不再由调用线程的对象所管理(意味着调用线程不用再使用join方法),让该线程自己独立的运行。两个线程均不再阻塞,同步执行。当一个线程结束执行时,回释放它自己使用的资源。当调用了这个方法后,线程对象变成non-joinable状态,可以被安全删除。
例子
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
} int main()
{
std::cout << "Spawning and detaching 3 threads...\n";
std::thread (pause_thread,1).detach();
std::thread (pause_thread,2).detach();
std::thread (pause_thread,3).detach();
std::cout << "Done spawning threads.\n"; std::cout << "(the main thread will now pause for 5 seconds)\n";
// give the detached threads time to finish (but not guaranteed!):
pause_thread(5);
return 0;
}
结果
Spawning and detaching 3 threads...
Done spawning threads.
(the main thread will now pause for 5 seconds)
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
pause of 5 seconds ended
(2)id get_id() const noexcept;
取得线程id
如果线程对象是joinable的,这个方法返回一个唯一的线程id。
如果线程对象不是joinable的,这个方法返回默认构造对象的id(为0)。
(3)void join();
调用这个函数的主线程会被阻塞直到子线程结束,子线程结束后有一些资源通过主线程回收。当调用这个函数之后,子线程对象变成non-joinable状态,可以安全销毁。该函数目的是防止主线程在子线程结束前销毁,导致资源未成功回收。
例子
// example for thread::join
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
} int main()
{
std::cout << "Spawning 3 threads...\n";
std::thread t1 (pause_thread,1);
std::thread t2 (pause_thread,2);
std::thread t3 (pause_thread,3);
std::cout << "Done spawning threads. Now waiting for them to join:\n";
t1.join();
t2.join();
t3.join();
std::cout << "All threads joined!\n"; return 0;
}
结果
Spawning 3 threads...
Done spawning threads. Now waiting for them to join:
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
All threads joined!
(4)bool joinable() const noexcept;
返回线程的joinable状态。
如果一个线程对象是作为一个线程执行的它就是joinable的。
当线程是如下情况的时候就不是joinable的
a. 如果是用默认构造函数产生的(未执行)。
b. 如果它曾被作为移动构造函数的参数(x)。
c. 如果它曾被join或者detach过。
(5)native_handle_type native_handle();
返回A value of member type thread::native_handle_type.
待续…
c++11: <thread>学习的更多相关文章
- C++11 学习笔记 std::function和bind绑定器
C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...
- C++ 11 学习1:类型自动推导 auto和decltype
Cocos 3.x 用了大量的C++ 11 的东西,所以作为一个C++忠实粉丝,有必要对C++ 11进行一个系统的学习. 使用C++11之前,一定要注意自己使用的编译器对C++11的支持情况,有些编译 ...
- C++11学习
转自: https://www.cnblogs.com/llguanli/p/8732481.html Boost教程: http://zh.highscore.de/cpp/boost/ 本章目的: ...
- C++11学习之share_ptr和weak_ptr
一.shared_ptr学习 1.shared_ptr和weak_ptr 基础概念 shared_ptr与weak_ptr智能指针均是C++ RAII的一种应用,可用于动态资源管理 shared_pt ...
- Linux0.11学习
Linux 0.11虽然不是什么“珠穆朗玛峰”,但它肯定还是“华山”或“泰山”.虽然有路但你还是需要最基本的努力和花费一定的代价才能“攀登”上去.1. PC兼容机硬件工作原理(比如8259A,8253 ...
- C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)
因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...
- C++11学习笔记
C++11 1.long long新类型 2.列表初始化 int t=0; int t={0}; int t(0); int t{0}; 注意:如果我们使用列表初始化有丢失信息的风险,则编译器报错 l ...
- C++ 11学习(1):lambda表达式
转载请注明,来自:http://blog.csdn.net/skymanwu #include <iostream> #include <vector> #include &l ...
- C++ 11 学习3:显示虚函数重载(override)
5.显示虚函数重载 在 C++ 里,在子类中容易意外的重载虚函数.举例来说: struct Base { virtual void some_func(); }; struct Derived : B ...
- C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)
3.空指针(nullptr) 早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分. C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别 ...
随机推荐
- Android使用webService
在android中使用webservice,首先要导入Android webservice支持包 ksoap2-android-assembly-3.3.0-jar-with-dependencies ...
- Integer to Roman(JAVA)
public String intToRoman(int num) { int[] values={1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[ ...
- C#中Func<T,TResult>的用法和Lambda表达式
在C#3.0中引用了Limbda表达式,Limbda表达式实际上就是一个方法,只不过该方法是匿名方法(即没有名称的方法)代码片段: Func<int,string,string> t=(i ...
- jQuery.on() 函数详解
on() 函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于 ...
- C++中vector和list排序
容器.泛型算法.和类是不是就是C++相对于C"++"的那部分呢?暂时先这么认为吧.如果这篇博客有幸被别人看到,请帮忙指出.--C++ 菜鸟 留. vector的迭代器是随机访问迭代 ...
- 动态链接库的生成(dll)和 动态链接库隐式and显式调用
一.构建动态链接库(dll.dll dll.lib dll.h) 说明: .dll 是在执行程序是调用 .lib 是在连接程序是调用 .h是在编译程序时调用 1.头文件(声明导入函数):_decl ...
- 菜单之二:使用xml文件定义菜单
参考<疯狂android讲义>2.10节 P174,参见归档project:XmlMenuDemo.zip 一般推荐使用XML文件定义菜单. 基本步骤如下: 1.定义布局文件 为简单显示原 ...
- mybatis常用jdbcType数据类型
MyBatis 通过包含的jdbcType类型 BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED ...
- Windows 下 Apache HTTP Server 安装、配置以及与 Tomcat 的整合(附图)
如果您能点开这篇文章,说明您已对熟悉Apache HTTP Server(下文用Apache简称)配置的重要性已很清楚了,本文不在赘述,直接介入正题,请往下阅读: 为便于阅读,列出文章目录: 一.Ap ...
- 使用jsonp实现ajax跨域请求
Jsonp(JSON with Padding)是资料格式 json 的一种“使用模式”,可以让网页从别的网域获取资料. 由于同源策略,一般来说位于 server1.example.com 的网页无法 ...