std::thread::thread
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| thread(); |
(1) | (C++11 起) |
| thread( thread&& other ); |
(2) | (C++11 起) |
| template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); |
(3) | (C++11 起) |
| thread(const thread&) = delete; |
(4) | (C++11 起) |
。构建新的线程对象.
Original:
Constructs new thread object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
。创建新的线程对象,这并不代表一个线程.
Original:
Creates new thread object which does not represent a thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
。移动的构造函数。构造的线程对象代表,代表
other执行的线程。在这个调用之后other不再代表一个线程的执行.Original:
Move constructor. Constructs the thread object to represent the thread of execution that was represented by
other. After this call other no longer represents a thread of execution.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
。创建新的
std::thread对象,并将它与一个线程执行。首先,构造函数的所有参数复制args...仿佛线程局部存储的功能。Original:
Creates new
std::thread object and associates it with a thread of execution. First the constructor copies all arguments args... to thread-local storage as if by the function:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
template <class T> typename decay<T>::type decay_copy(T&& v) { return std::forward<T>(v); }
。 @ @在评价和复制过程中的参数抛出的任何异常被抛出在当前线程中,而不是新的线程.
Original:
@@ Any exceptions thrown during evaluation and copying of the arguments are thrown in the current thread, not the new thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
。 @ @将在新的线程中运行的代码定义如下。
让我们参考,以None
。
copied_args作为t1,t2,...,tN,N是sizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread:Original:
Let's refer to None
copied_args as t1, t2, ..., tN, where N is sizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
-
f是T类的成员函数的指针,那么它被称为。返回值将被忽略。实际上,下面的代码被执行Original:Iffis pointer to a member function of classT, then it is called. The return value is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- (t1.*f)(t2, ..., tN)的类型
t1要么是T,参考T或参考输入来自T.Original:(t1.*f)(t2, ..., tN) if the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - ((*t1).*f)(t2, ..., tN)否则.....Original:((*t1).*f)(t2, ..., tN) otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- 如果N == 1和
f指针的类对象的成员数据,然后进行访问。该对象的值被忽略。实际上,下面的代码被执行Original:If N == 1 andfis pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- t1.*f如果和类型
t1要么是T,参照T或参照键入来自T.Original:t1.*f if and the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - (*t1).*f否则.....Original:(*t1).*f otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
-
f被称为在所有其他情况下,一个非成员函数的指针。返回值将被忽略。实际上,f(t1, t2, ..., tN)执行.Original:fis called as a pointer to a non-member function in all other cases. The return value is ignored. Effectively, f(t1, t2, ..., tN) is executed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
@@ The code that will be run in the new thread is defined as follows.
让我们参考,以None
copied_args作为t1,t2,...,tN,N是sizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread:Original:
Let's refer to None
copied_args as t1, t2, ..., tN, where N is sizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
-
f是T类的成员函数的指针,那么它被称为。返回值将被忽略。实际上,下面的代码被执行Original:Iffis pointer to a member function of classT, then it is called. The return value is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- (t1.*f)(t2, ..., tN)的类型
t1要么是T,参考T或参考输入来自T.Original:(t1.*f)(t2, ..., tN) if the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - ((*t1).*f)(t2, ..., tN)否则.....Original:((*t1).*f)(t2, ..., tN) otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- 如果N == 1和
f指针的类对象的成员数据,然后进行访问。该对象的值被忽略。实际上,下面的代码被执行Original:If N == 1 andfis pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- t1.*f如果和类型
t1要么是T,参照T或参照键入来自T.Original:t1.*f if and the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - (*t1).*f否则.....Original:(*t1).*f otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
-
f被称为在所有其他情况下,一个非成员函数的指针。返回值将被忽略。实际上,f(t1, t2, ..., tN)执行.Original:fis called as a pointer to a non-member function in all other cases. The return value is ignored. Effectively, f(t1, t2, ..., tN) is executed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
。被删除,拷贝构造函数,线程是不是复制的。没有两个
std::thread对象可以代表同一个线程的执行.Original:
The copy constructor is deleted; threads are not copyable. No two
std::thread objects may represent the same thread of execution.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 。参数。
| other | - | 。
另一个线程对象来构造这个线程对象。
Original: another thread object to construct this thread object with The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| f | - | 。在新的线程要执行的函数。
Original: function to execute in the new thread The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| args... | - | 。参数传递给新的功能。
Original: arguments to pass to the new function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 。例外。
1-2)
3)
。 std::system_error如果线程无法启动。唯一的例外可能是错误条件
std::errc::resource_unavailable_try_again或其他特定于实现的错误条件.Original:
std::system_error if the thread could not be started. The exception may represent the error condition
std::errc::resource_unavailable_try_again or another implementation-specific error condition.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 。注释。
。复制给线程函数的参数值。如果需要传递给线程函数的引用参数,它必须包装(如与std::ref或std::cref)。
Original:
The arguments to the thread function are copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g. with std::ref or std::cref).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
。从功能上任何返回值将被忽略。如果函数抛出一个异常,被称为std::terminate。为了通过返回值或异常调用线程,std::promise或std::async可用于.
Original:
Any return value from the function is ignored. If the function throws an exception, std::terminate is called. In order to pass return values or exceptions back to the calling thread, std::promise or std::async may be used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 。为例。
#include <iostream> #include <utility> #include <thread> #include <chrono> #include <functional> #include <atomic> void f1(int n) { for(int i=0; i<5; ++i) { std::cout << "Thread " << n << " executing\n"; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } void f2(int& n) { for(int i=0; i<5; ++i) { std::cout << "Thread 2 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int main() { int n = 0; std::thread t1; // t1 is not a thread std::thread t2(f1, n+1); // pass by value std::thread t3(f2, std::ref(n)); // pass by reference std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread t2.join(); t4.join(); std::cout << "Final value of n is " << n << '\n'; }
Possible output:
Thread 1 executing Thread 2 executing Thread 1 executing Thread 2 executing Thread 1 executing Thread 2 executing Thread 1 executing Thread 2 executing Thread 1 executing Thread 2 executing Final value of n is 5