设为首页 加入收藏

TOP

C++11 thread(1)(一)
2015-07-20 17:45:27 来源: 作者: 【 】 浏览:2
Tags:thread
?

std::thread::thread

default (1)
thread() noexcept;
initialization (2)
template 
       
        
explicit thread (Fn&& fn, Args&&... args);

       
copy [deleted] (3)
thread (const thread&) = delete;
move (4)
thread (thread&& x) noexcept;
Construct thread Constructs a thread object:
构造一个线程对象。

Construct a thread object that does not represent any thread of execution.

构造一个不执行任何线程的线程对象。

例子:

?

#include 
   
    
#include 
    
      using namespace std; int main() { thread t; cout<
     
      运行情况:
      

?

\

可以看到,t是处于non-executing(未执行)状态的。

?

Construct a thread object that represents a new joinable thread of execution.

The completion of this construction synchronizes with the beginning of the invocation of this copy of fn.

构造一个新的可等待的线程对象。

新的线程将调用函数fn并将args传递给fn.

该构造完成与fn的复制完成同步。(即fn复制完该构造就完成了)

例子:

?

#include 
       
        
#include 
        
          #include 
         
           using namespace std; void show(int n){ cout<
          
           运行截图:
           

?

\

?

?

Deleted constructor form (thread objects cannot be copied).

默认被Delete的构造器。(线程对象不能被复制)

(3) copy constructor

从执行中的线程x中构造。该操作不会以任何方式影响执行中的线程,只会移交其控制权。

x不再代理任何线程的执行。

例子:

?

#include 
            
             
#include 
             
               #include 
              
                #include 
               
                 using namespace std; //delay(n) 延时n秒 void delay(double sec) { time_t start_time, cur_time; // 变量声明 time(&start_time); do { time(&cur_time); }while((cur_time - start_time) < sec ); }; void show(int n){ while(n>5){ cout<
                
                 运行截图:
                 

?

\ 这是我电脑上运行一次的结果,结果不一定一样。

?

thread objects that are joinable shall either be joined or detached before they are destroyed.

可连接线程对象在销毁之前应该被joined或者detached.

?

Parameters

fn
A pointer to function, pointer to member, or any kind of move-constructible function object (i.e., an object whose class defines operator(), including closures and function objects).

The return value (if any) is ignored.

fn为一个指向函数的指针,或者一个指向成员的的指针,甚至是任一的可以被移动构造的函数对象。

fn的返回值会被忽略。

args...
Arguments passed to the call to fn (if any). Their types shall be move-constructible.

If fn is a member pointer, the first argument shall be an object for which that member is defined (or a reference, or a pointer to it).

fn函数的参数。

thread object whose state is moved to the constructed object.

x是一个线程对象。

Fn,Args是模版参数,如果是隐式推导,那么这会绑定相应的左值或者右值,fn将会在新线程中被调用。

?

Example

?

Data races

Exception safety

exception type error condition description
system_error errc::resource_unavailable_try_again The system is unable to start a new thread

如果复制过程出错,会抛出system_error错误。

?

Note that if an exception is thrown from the function invocation (i.e., from fn itself), it is handled by the new thread. If this invocation terminates with an uncaught exception, terminate() is called.

如果fn抛出异常,线程将会将控制权移交给新的线程,如果不捕获异常,将调用terminate.

?

?

——————————————————————————————————————————————————————————————————

?

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

?

2014-9-3

于GDUT

——————————————————————————————————————————————————————————————————

?

?

x Fn and Args... are template parameters: if implicitly deduced, these are the proper lvalue or rvalue reference type to bind the arguments to. Note though, that on the call to fn in the new thread, decay copies of fn and args... are always used (see std::ref for a wrapper class that makes references copyable). Output: The move constructor (4) modifies x. The initialization constructor (2) throws an exception on the foll
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU-4841 圆桌问题 STL模拟约瑟夫.. 下一篇uva 11855 - Buzzwords(后缀数组)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何利用Python做数 (2025-12-24 23:48:36)
·如何使用python进行 (2025-12-24 23:48:34)
·python 爬虫入门该怎 (2025-12-24 23:48:31)
·Java 实现多个大文件 (2025-12-24 23:22:00)
·Java多线程编程在工 (2025-12-24 23:21:56)