原文地址:http://www.cplusplus.com/reference/thread/thread/thread/
public member function
<thread>
std::thread::thread
default (1)
thread() noexcept;
|
initialization (2)
template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);
|
copy [deleted] (3)
thread (const thread&) = delete;
|
move (4)
thread (thread&& x) noexcept;
|
Construct thread
Constructs athreadobject:
构造一个线程对象。
(1) default constructor
Construct athreadobject that does not represent
any thread of execution.
构造一个不执行任何线程的线程对象。
例子:
#include <iostream>
#include <thread>
using namespace std;
int main()
{
thread t;
cout<<"t.id="<<t.get_id()<<endl;
}
运行情况:
可以看到,t是处于non-executing(未执行)状态的。
(2) initialization constructor
Construct athreadobject that represents a newjoinablethread
of execution.
The new thread of execution callsfnpassingargsas arguments (usingdecay copiesof
itslvalueorrvaluereferences).
The completion of this construction synchronizes with the beginning of the invocation of this copy offn.
构造一个新的可等待的线程对象。
新的线程将调用函数fn并将args传递给fn.
该构造完成与fn的复制完成同步。(即fn复制完该构造就完成了)
例子:
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void show(int n){
cout<<"Now n is "<<n<<endl;
}
int main()
{
int m=10;
thread t(show,99);
t.join();//使t能执行完
cout<<"ok!"<<endl;
}
运行截图:
(3) copy constructor
Deleted constructor form (threadobjects cannot
be copied).
默认被Delete的构造器。(线程对象不能被复制)
(4) move constructor
Construct athreadobject that acquires the thread of execution represented byx(if
any). This operation does not affect the execution of the moved thread in any way, it simply transfers its handler.
Thexobject no longer represents any thread of execution.
从执行中的线程x中构造。该操作不会以任何方式影响执行中的线程,只会移交其控制权。
x不再代理任何线程的执行。
例子:
#include <iostream>
#include <thread>
#include <vector>
#include <ctime>
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<<"currentThread is "<<pthread_self()<<",Now n is "<<n<<endl;
delay(0.5);
n--;
}
}
thread returnThread(){
cout<<"returnThread start!"<<endl;
thread t(show,10);
delay(2);
cout<<"returnThread called!"<<endl;
return t;
}
int main()
{
thread t2(returnThread());
t2.join();
cout<<"ok!"<<endl;
}
运行截图:
这是我电脑上运行一次的结果,结果不一定一样。
threadobjects that arejoinableshall
either bejoinedordetachedbefore
they aredestroyed.
可连接线程对象在销毁之前应该被joined或者detached.
Parameters
fn
A pointer to function, pointer to member, or any kind ofmove-constructiblefunction
object (i.e., an object whose class definesoperator(), includingclosuresandfunctionobjects).
The return value (if any) is ignored.
fn为一个指向函数的指针,或者一个指向成员的的指针,甚至是任一的可以被移动构造的函数对象。
fn的返回值会被忽略。
args...
Arguments passed to the call tofn(if any). Their types shall bemove-constructible.
Iffnis amember pointer, the first argument shall be an object for
which that member is defined (or a reference, or a pointer to it).
fn函数的参数。
x
threadobject whose state
is moved to the constructed object.
x是一个线程对象。
Fn
andArgs...
are template parameters: ifimplicitlydeduced, these are the properlvalueorrvaluereference
type to bind the arguments to. Note though, that on the call tofnin the new thread,decay
copiesoffnandargs...are always used (seestd::reffor
a wrapper class that makes references copyable).
Fn,Args是模版参数,如果是隐式推导,那么这会绑定相应的左值或者右值,fn将会在新线程中被调用。
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
// 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;
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;
}
|
|
Output:
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
|
Data races
Themove constructor (4)modifiesx.
Exception safety
The initialization constructor(2)throws an exception on the following conditions:
It also throws if the construction of any of the copies it makes (of thedecay typesofFnandArgs...)
throws.
Depending on the library implementation, this constructor may also throw exceptions to report other situations (such asbad_allocorsystem_errorwith
othererror conditions).
如果复制过程出错,会抛出system_error错误。
Note that if an exception is thrown from the function invocation (i.e., fromfnitself), 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
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
C++11 线程池 ThreadPool 单例 线程池,实现全局唯一。
A simple C++11 Thread Pool implementation. Basic usage: // create thread pool with 4 worker threads ThreadPool pool(4); // enqueue and store future auto result = pool.enqueue([](int answer) { ...
Qt和C++11thread.7zQt和C++11thread.7zQt和C++11thread.7z
C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的类、用于互斥访问的类与方法等。应用C++11中的std::thread便于...
线程池c++11 thread类
Qt例程和C++11thread.7z
c++11在语言层面上提供了对thread的支持,由于不同的平台提供了不同线程API,在语言层面提供了对thread的支持可以大大的减小代码移植的工作量。 本文将给大家详细介绍关于c++11封装thread库的相关内容,下面话不多说...
A simple C++11 Thread Pool implementation
c++ std thread教程
该资源为c++11版本的线程池实现,其中调用了c++11相关的新特性,例如thread、function等,推荐新手下载使用!
基于Linux平台,C++开发,socket服务器之线程池,使用thread,同步利用mutex和condition_variable实现
C++语言学习资料,多线程方面的(里面有文档以及代码)
c++11多线程库的使用,主要介绍了thread类中的构造函数 join函数和detach函数的使用方式
C++ Boost Thread 编程指南 0分上船
内存模型 为 C++ 抽象机的目的定义计算机内存存储的语义。 可用于 C++ 程序的内存是一或多个相接的字节序列...执行线程是程序中的控制流,它始于 std::thread::thread 、 std::async 或以其他方式所做的顶层函数调用。
Thread pool implementation using c++11 threads
c++ thread event c++ thread event c++ thread event
c++11的多线程库使用方法例子,介绍了c++11和thread库的使用方法
c++ c11 thread lock-free