std::thread::thread
De 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) | (desde C++11) |
thread( thread&& other ); |
(2) | (desde C++11) |
template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); |
(3) | (desde C++11) |
thread(const thread&) = delete; |
(4) | (desde C++11) |
Constrói objeto novo segmento.
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)
Cria objeto novo segmento que não representa um fio.
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)
Mova construtor. Constrói o objeto de discussão para representar o segmento de execução que foi representada por
other. Após esta chamada other já não representa um fio de execução.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)
Cria novo
std::thread objeto e associa-lo com um fio de execução. Primeiro o construtor copia todos args... argumentos para segmento local de armazenamento como se pela função: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);
}
@ @ As exceções lançadas durante a avaliação e cópia dos argumentos são jogados no segmento atual não, o novo segmento.
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.
@ @ O código que será executado no novo segmento é definido como segue. Vamos referir a None
copied_args como t1, t2,..., tN, onde 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:
- Se
fé ponteiro para uma função membro deTclasse, então ele é chamado. O valor de retorno é ignorado. Efetivamente, o seguinte código é executado: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)se o tipo det1é ouT, a referência aTou referência ao tipo derivado deT.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)de outra forma.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.
- Se
N == 1efé ponteiro para um objeto de dados membro de uma classe, então ela é acessada. O valor do objeto é ignorado. Efetivamente, o seguinte código é executado:Original:IfN == 1andfis 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.*fse e o tipo det1é ouT, a referência aTou referência ao tipo derivado deT.Original:t1.*fif 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).*fde outra forma.Original:(*t1).*fotherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
fé chamado como um ponteiro para uma função não-membro em todos os outros casos. O valor de retorno é ignorado. Efectivamente, é executadof(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. Vamos referir a None
copied_args como t1, t2,..., tN, onde 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:
- Se
fé ponteiro para uma função membro deTclasse, então ele é chamado. O valor de retorno é ignorado. Efetivamente, o seguinte código é executado: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)se o tipo det1é ouT, a referência aTou referência ao tipo derivado deT.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)de outra forma.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.
- Se
N == 1efé ponteiro para um objeto de dados membro de uma classe, então ela é acessada. O valor do objeto é ignorado. Efetivamente, o seguinte código é executado:Original:IfN == 1andfis 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.*fse e o tipo det1é ouT, a referência aTou referência ao tipo derivado deT.Original:t1.*fif 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).*fde outra forma.Original:(*t1).*fotherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
fé chamado como um ponteiro para uma função não-membro em todos os outros casos. O valor de retorno é ignorado. Efectivamente, é executadof(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)
O construtor de cópia é apagado; tópicos não são copiável. Não há duas
std::thread objetos podem representar o mesmo segmento de execução.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.
Parâmetros
| other | - | outro objeto de discussão para construir este objeto de discussão com
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 | - | função para executar no novo segmento
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... | - | argumentos para passar para a nova função
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. |
Exceções
1-2)
3)
std::system_error se o segmento não pôde ser iniciado. A exceção pode representar a
std::errc::resource_unavailable_try_again condição de erro ou outra condição de erro aplicação específica.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.
Notas
Os argumentos para a função segmento são copiados por valor. Se um argumento de referência tem de ser passado para a função de rosca, que tem que ser envolvida (por exemplo, com ou 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.
Qualquer valor de retorno da função é ignorado. Se a função lança uma exceção, std::terminate é chamado. A fim de passar valores de retorno ou exceções de volta para a chamada linha std::promise, ou std::async pode ser usado.
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.
Exemplo
#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';
}
Potencial saída:
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