<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. Winform单例模式与传值

    单例模式(singleton)的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 在多窗体界面中,如果要加入一个“关于”的窗体,用于显 ...

  2. js 去掉空格

    写成类的方法格式如下:(str.trim();)<script language="javascript"> String.prototype.trim=functio ...

  3. es3中使用es6/7的字符串扩展

    最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ...

  4. (转载)C++之tinyXML使用

     tinyXML一款很优秀的操作C++类库,文件不大,但方法很丰富,和apache的Dom4j可以披靡啊!习惯了使用java类库的我看到这么丰富的c++类库,很高兴!它使用很简单,只需要拷贝几个文件到 ...

  5. commands - `ls`

    list only directories: ls -d /path/to/*/

  6. google visit

    http://emuch.net/bbs/viewthread.php?tid=7630684&fpage=3&target=blank 内Facebook,twitter,dropb ...

  7. 日志管理-NLog日志框架简写用法

    本文转载:http://www.blogjava.net/qiyadeng/archive/2013/02/27/395799.html 在.net中也有非常多的日志工具,今天介绍下NLog.NLog ...

  8. Font Awesome 4.0.3 字体图标完美兼容IE7

    1.下载Font Awesome 4.0.3兼容包,http://www.thinkcmf.com/index.php?m=font 2.解压,并放到自己网站系统合适的位置(如果你的站已使用Font ...

  9. Yii 2.0安装

    通过 Composer 安装 注意: php版本最好在5.5以上! 1.下载 Yii2的高级应用程序模板 ,然后将其解压缩到一个Web可访问的文件夹. 2.下载Composer-Setup.exe , ...

  10. Python成长之路_装饰器

    一.初入装饰器 1.首先呢我们有这么一段代码,这段代码假设是N个业务部门的函数 def f1(aaa): print('我是F1业务') if aaa == 'f1': return 'ok' def ...