<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的线程在它们被销毁前需要被joindetach(否则线程资源不会被完全释放)。

参数介绍:

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>学习的更多相关文章

  1. C++11 学习笔记 std::function和bind绑定器

    C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...

  2. C++ 11 学习1:类型自动推导 auto和decltype

    Cocos 3.x 用了大量的C++ 11 的东西,所以作为一个C++忠实粉丝,有必要对C++ 11进行一个系统的学习. 使用C++11之前,一定要注意自己使用的编译器对C++11的支持情况,有些编译 ...

  3. C++11学习

    转自: https://www.cnblogs.com/llguanli/p/8732481.html Boost教程: http://zh.highscore.de/cpp/boost/ 本章目的: ...

  4. C++11学习之share_ptr和weak_ptr

    一.shared_ptr学习 1.shared_ptr和weak_ptr 基础概念 shared_ptr与weak_ptr智能指针均是C++ RAII的一种应用,可用于动态资源管理 shared_pt ...

  5. Linux0.11学习

    Linux 0.11虽然不是什么“珠穆朗玛峰”,但它肯定还是“华山”或“泰山”.虽然有路但你还是需要最基本的努力和花费一定的代价才能“攀登”上去.1. PC兼容机硬件工作原理(比如8259A,8253 ...

  6. C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)

    因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...

  7. C++11学习笔记

    C++11 1.long long新类型 2.列表初始化 int t=0; int t={0}; int t(0); int t{0}; 注意:如果我们使用列表初始化有丢失信息的风险,则编译器报错 l ...

  8. C++ 11学习(1):lambda表达式

    转载请注明,来自:http://blog.csdn.net/skymanwu #include <iostream> #include <vector> #include &l ...

  9. C++ 11 学习3:显示虚函数重载(override)

    5.显示虚函数重载 在 C++ 里,在子类中容易意外的重载虚函数.举例来说: struct Base { virtual void some_func(); }; struct Derived : B ...

  10. C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)

    3.空指针(nullptr) 早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分. C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别 ...

随机推荐

  1. material design 图标制作参数

    可用图标的标准不透明度在亮色背景上是54%(#000000).可视等级较低的禁用图标的不透明度应为 26%(#000000). 可用图标的标准不透明度在暗色背景上是 100%(#FFFFFF).可视等 ...

  2. iOS开发那些事儿(一)轮播器

    前言 市面上绝大部分的APP被打开之后映入眼帘的都是一个美轮美奂的轮播器,所以能做出一个符合需求.高效的轮播器成为了一个程序员的必备技能.所以今天的这篇博客就来谈谈轮播器这个看似简单的控件其中蕴含的道 ...

  3. NSNumber 和 NSValue 的部分使用

    1.NSNumber 在Objective-c中有int,float,char等基本数据类型,但这些基本数据类型并不是对象,而数组,字典,字符串等容器中存放的都是对象类型,因此我们需要用到NSNumb ...

  4. typedef和define的作用域

    typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用 ...

  5. java static关键字

    方便在没有创建对象的情况下来进行调用(方法/变量). 很显然,被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问. static可以用来修饰类的 ...

  6. sqlplus 链接数据库

    实验目的:在虚拟机中用sqlplus工具访问真实机的数据库: 实验环境: 真实机(windows系统,数据库服务名 orcl): SQL> select * from v$version; BA ...

  7. apache AH01630: client denied by server configuration错误解决方法

    今天本来是想要在自己本地搭建一个wamp环境用来做一些代码的测试和框架的学习. 鉴于目前工作的时候用到了php5.5,所以就用了wamp-server V2.5版本,安装完成之后配置虚拟主机一直出现4 ...

  8. 解决nginx上传模块nginx_upload_module传递GET参数

    解决nginx上传模块nginx_upload_module传递GET参数的方法总结 最近用户反映我们的系统只能上传50M大小的文件, 希望能够支持上传更大的文件. 很显然PHP无法轻易实现大文件上传 ...

  9. Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析

    原文:Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析 前段时间,公司同事开发了一个小工具,在工具执行过程中,UI界面一直处于卡死状态. 通过阅读代码发现,主要是 ...

  10. thinkPHP模板引擎案例

    1.if <if condition="$vo.business eq LS"> 零售 <elseif condition="$vo.business ...