C++11中的使用

               

C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装。std::thread代表了一个线程对象。应用C++11中的std::thread便于多线程程序的移值。

<thread>是C++标准程序库中的一个头文件,定义了C++11标准中的一些表示线程的类、用于互斥访问的类与方法等。

类std::thread表示一个线程。初始化时给出该线程的执行函数(或是可以调用的对象)。线程对象构造后即开始运行。默认情况下,C++11的子线程必须与主线程会合,即在主线程中调用thread::join()函数,这避免了子线程还在执行,主线程已经执行结束而撤销的情况。

如果子线程的执行函数需要参数,可把实参列表写在std::thread对象构造函数的参数列表中。如果把可调用对象(callable object)作为参数传给子线程的构造函数,则把该可调用对象复制一份给子线程。如果需要传递可调用对象的左值引用给子线程,则采用std::ref()来产生对象的引用、然后把引用值再传进去给子线程。

C++11所定义的线程是和操作系统的线程一一对应的,也就是说我们生成的线程都是直接接受操作系统的调度的,一个进程所能创建的线程数目以及一个操作系统所能创建的总的线程数据等都由运行时操作系统限定。

std::thread中主要声明三类函数:(1)、构造函数、拷贝构造函数(拷贝构造函数被禁用,意味着thread不可被拷贝构造,但能被转移(move)或者互换(swap))及析构函数;(2)、成员函数;(3)、静态成员函数(hardware_concurrency,检测硬件并发特性, Returns the number of hardware thread contexts)。

std::thread类成员函数:

(1)、get_id:获取线程ID,返回一个类型为std::thread::id的对象。

(2)、joinable:检查线程是否可被join。检查thread对象是否标识一个活动(active)的可行性线程。缺省构造的thread对象、已经完成join的thread对象、已经detach的thread对象都不是joinable。

(3)、join:调用该函数会阻塞当前线程。阻塞调用者(caller)所在的线程直至被join的std::thread对象标识的线程执行结束。

(4)、detach:将当前线程对象所代表的执行实例与该线程对象分离,使得线程的执行可以单独进行。一旦线程执行完毕,它所分配的资源将会被释放。

(5)、native_handle:该函数返回与std::thread具体实现相关的线程句柄。native_handle_type是连接thread类和操作系统SDK API之间的桥梁,如在Linux g++(libstdc++)里,native_handle_type其实就是pthread里面的pthread_t类型,当thread类的功能不能满足我们的要求的时候(比如改变某个线程的优先级),可以通过thread类实例的native_handle()返回值作为参数来调用相关的pthread函数达到目录。This member function is only present in class thread if the library implementation supports it. If present, it returns a value used to access implementation-specific information associated to the thread.

(6)、swap:交换两个线程对象所代表的底层句柄。

(7)、operator=:moves the thread object

(8)、hardware_concurrency:静态成员函数,返回当前计算机最大的硬件并发线程数目。基本上可以视为处理器的核心数目。

另外,std::thread::id表示线程ID,定义了在运行时操作系统内唯一能够标识该线程的标识符,同时其值还能指示所标识的线程的状态。Values of this type are returned by thread::get_id and this_thread::get_id to identify threads.

有时候我们需要在线程执行代码里面对当前调用者线程进行操作,针对这种情况,C++11里面专门定义了一个命名空间this_thread,此命名空间也声明在<thread>头文件中,其中包括get_id()函数用来获取当前调用者线程的ID;yield()函数可以用来将调用者线程跳出运行状态,重新交给操作系统进行调度,即当前线程放弃执行,操作系统调度另一线程继续执行;sleep_until()函数是将线程休眠至某个指定的时刻(time point),该线程才被重新唤醒;sleep_for()函数是将线程休眠某个指定的时间片(time span),该线程才被重新唤醒,不过由于线程调度等原因,实际休眠实际可能比sleep_duration所表示的时间片更长。

std::thread:Class to represent individual threads of execution. A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.

std::this_thread:This namespace groups a set of functions that access the current thread.

下面是从其他文章中copy的<thread>测试代码,详细内容介绍可以参考对应的reference:

#include "thread2.hpp"#include <iostream>#include <vector>#include <functional>#include <memory>#include <list>#include <mutex>#include <condition_variable>#include <atomic>#include <thread>#include <chrono>#include <iomanip>#include <ctime>#include <algorithm>namespace thread_ {
   std::atomic<int> global_counter(0);#ifdef _MSC_VER///// reference: http://www.cplusplus.com/reference/thread/thread/thread/static void increase_global(int n) { for (int i = 0; i<n; ++i) ++global_counter; }static 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 test_thread_thread()// thread::thread: Constructs a thread object 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::
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值