<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. C#自定义控件在添加引用后不显示在工具箱的解决方法

    先说一些背景: 在开发C#项目时,发现很多控件存在复用的情况,控件的属性都是要设置成一样的,我就想,能不能设置一个类来存放这个控件,这样我每次用的时候直接加一些特殊的操作就可以了,不需要再次设置控件属 ...

  2. andrid中的Sqlite 数据库连接(本地版)

    sqlite简介 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前 ...

  3. React-Native 获取node.js提供的接口

    一个简单的React-Native 获取node.js提供的接口的实现 一.node.js var http = require("http"); var url = requir ...

  4. select count(distinct a)

    我想对一个表里面字段a的个数进行进行统计,因为字段a有重复的记录,我想排除重复的记录,该sql语句为: select count(distinct a) 链接:http://www.w3school. ...

  5. sql 练习(1)

    1.建立实验表 CREATE TABLE STUDENT (SNO ) NOT NULL, SNAME ) NOT NULL, SSEX ) NOT NULL, SBIRTHDAY DATE, CLA ...

  6. 防止输入时键盘覆盖掉textfiled

    添加监听者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwasChange:) ...

  7. HDU 1088 - Write a simple HTML Browser

    直接看sample input = = 又一道模拟. #include <iostream> #include <string> #include <cstdio> ...

  8. 安装4.x版本的express开发框架

    错误版本(未更新前的安装方法,更新后就不是这么安装了,好多网上的教程还是这种方法,所以这里先写明了,旧版这里是可以通过的,但是新版4.x就不行了,请用分割线下边的方法)   本文演示在Linux上安装 ...

  9. onclick用法 超链接简单弹出窗口实例

    问题 需要异步处理不同状态 1. onclick用法 if判断弹出窗口 解题思路 1. onclick用法 if判断弹出窗口 注意.. <a href="javascript:void ...

  10. php in_array 和 str_replace

    有这样的用法哦 $allow_state_array_invoc = array('store_invoice','invoiceno','invoicerec','invoiceing'); if ...