首页 > 编程语言 > 详细

C++11 介绍 thread

时间:2014-01-20 08:51:13      阅读:808      评论:0      收藏:0      [点我收藏+]

本文章的目的是记录自己在学习C++11多线程模块之中的练习。

                      2014-01-19

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.

An initialized thread object represents an active thread of execution; Such a thread object is joinable, and has a unique thread id.

A default-constructed (non-initialized) thread object is not joinable, and its thread id is common for all non-joinable threads.

A joinable thread becomes not joinable if moved from, or if either join or detach are called on them.

--------------------------------------------------------

Member types  
id                                 Thread id (public member type )
native_handle_type Native handle type (public member type )
Member functions  
(constructor)   Construct thread (public member function )
(destructor) Thread destructor (public member function )
operator=  Move-assign thread (public member function )
get_id Get thread id (public member function )
joinable Check if joinable (public member function )
join Join thread (public member function )
detach Detach thread (public member function )
swap Swap threads (public member function )
native_handle Get native handle (public member function )
hardware_concurrency [static] Detect hardware concurrency (public static member function )

从Menber functions说起:

(constructor)               构造函数  负责线程对象的初始化构造。

(destructor)               析构函数 负责线程对象的销毁。

operator=                       等于赋值运算符,采用付给 = 号左侧对象一个右值。

这是thread模块的等于运算符的声明

  

测试代码如下

  

测试结果

----------------------------------------------------------------------------

创建5个线程
线程创建完毕,等待这5个线程join()
暂停 1 s
暂停 2 s
暂停 3 s
暂停 4 s
暂停 5 s
所有线程完成join()
请按任意键继续. . .

 ----------------------------------------------------------------------------

detach()                          字面意识是--分离,使线程和主线程独立,主线程结束时不在等待线程是否结束。 

测试代码如下:

测试结果:

创建 3 个线程.
创建完成三个线程
(主线程暂停5秒)
暂停 1 s
暂停 2 s
暂停 3 s
暂停 5 s
请按任意键继续. . .

get_id                            获得当前线程的id号。

joinable()                        判断当前线程是否可以join(),如果可以join()则返回1,否则返回0。

join()                              join()当前线程,有句解释是 The function returns when the thread execution has completed.  如果线程执行完毕,返回该线程调用的函数。也就是结束线程调用的函数的意思。

swap()                            交换线程,如t1.swap(t2)。

native_handle()                返回当前句柄,句柄指的是一个核心对象在某一个进程中的唯一索引。

hardware_concurrency()    返回当前设备线程数,如i7,返回unsigned int 类型 8。

测试代码

 

  

测试结果如下:

创建 3 个线程.
0000009C
000000A8
000000B4
8
8
8
创建线程完成 现在等待他们join():
t1 可以阻塞
running t1.join()
暂停 1 s
t1 不可以阻塞
-------------------------
t2 可以阻塞
running t2.join()
暂停 2 s
t2 不可以阻塞
-------------------------
t3 可以阻塞
running t3.join()
暂停 3 s
t3 不可以阻塞
所有线程join()
请按任意键继续. . .

 

命名空间std::this_thread下的函数

get_id             Get thread id (function )

yield                Yield to other threads (function )

sleep_until       Sleep until time point (function )

sleep_for         Sleep for time span (function )

get_id                                   std::this_thread下的函数,在函数内调用,可以返回调用当前函数的线程号。

 

 

 

测试代码:

测试结果:

this_thread::get_id() ---9024
this_thread::get_id() ---8220
this_thread::get_id() ---4012
t1.get_id() 9024
t2.get_id() 8220
t3.get_id() 4012
暂停 1 s
暂停 2 s
暂停 3 s
所有线程join()
请按任意键继续. . .

yield                                      std::this_thread下的函数,暂时放弃一段 CPU 时间、让给其他线程使用。

测试代码:(比较哪个线程先算完1-100w的计数)

测试结果:

race of 10 threads that count to 1 million:
0382916754
请按任意键继续. . .

 所以,第一算完的是0号线程,第二名是3号,第三名是8号……

sleep_until                             std::this_thread下的函数,设置一个绝对时刻、让运行线程在指定的时刻后再继续运行。

sleep_for                               std::this_thread下的函数,停止一段指定的时长的执行。

sleep_for()

 

未完待续...

C++11 介绍 thread

原文:http://www.cnblogs.com/wk23/p/3525986.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!