std::call_once
Da 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. |
Definido no cabeçalho <mutex>
|
||
template< class Function, class... Args > void call_once( std::once_flag& flag, Function&& f, Args&& args... ); |
(desde C++11) | |
Executa a função
f
exatamente uma vez, mesmo se chamado de vários segmentos. Original:
Executes the function
f
exactly once, even if called from several threads. 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.
Cada grupo de invocações
call_once
que recebe o objeto std::once_flag mesmo irá atender os seguintes requisitos:Original:
Each group of
call_once
invocations that receives the same std::once_flag object will meet the following requirements: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.
- Exatamente uma execução de exatamente uma das funções (passado como
f
para as invocações do grupo) é realizada. É indefinido cuja função será seleccionada para execução. A função selecionada é executado no mesmo segmento como a invocaçãocall_once
foi passado para.Original:Exactly one execution of exactly one of the functions (passed asf
to the invocations in the group) is performed. It is undefined which function will be selected for execution. The selected function runs in the same thread as thecall_once
invocation it was passed to.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Nenhuma invocação nos retornos do grupo antes da execução acima mencionado da função seleccionada é completada com sucesso, isto é, não sai via uma excepção.Original:No invocation in the group returns before the abovementioned execution of the selected function is completed successfully, that is, doesn't exit via an exception.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se a função sai selecionados através de exceção, ela é propagada para o chamador. Outra função é então seleccionado e executado.Original:If the selected function exits via exception, it is propagated to the caller. Another function is then selected and executed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Índice |
[editar] Parâmetros
flag | - | um objeto, por que exatamente uma função é executada
Original: an object, for which exactly one function gets executed 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 a ser chamada
Original: function to call 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 função
Original: arguments to pass to the function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[editar] Valor de retorno
(Nenhum)
Original:
(none)
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.
[editar] Exceções
- std::system_error se qualquer condição impede chamadas para
call_once
de execução, conforme especificadoOriginal:std::system_error if any condition prevents calls tocall_once
from executing as specifiedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - qualquer exceção lançada pelo
f
Original:any exception thrown byf
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[editar] Exemplo
#include <iostream> #include <thread> #include <mutex> std::once_flag flag; void do_once() { std::call_once(flag, [](){ std::cout << "Called once" << std::endl; }); } int main() { std::thread t1(do_once); std::thread t2(do_once); std::thread t3(do_once); std::thread t4(do_once); t1.join(); t2.join(); t3.join(); t4.join(); }
Saída:
Called once
[editar] Veja também
(C++11) |
objeto auxiliar a assegurar que call_once invoca a função apenas uma vez Original: helper object to ensure that call_once invokes the function only once The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (classe) |