std::call_once
De cppreference.com
<metanoindex/>
<tbody> </tbody>| 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
fpara 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_oncefoi passado para.Original:Exactly one execution of exactly one of the functions (passed asfto 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_onceinvocation 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.
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. |
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.
Exceções
- std::system_error se qualquer condição impede chamadas para
call_oncede execução, conforme especificadoOriginal:std::system_error if any condition prevents calls tocall_oncefrom 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
fOriginal:any exception thrown byfThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
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
