std::call_once
Aus 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. |
definiert in Header <mutex>
|
||
template< class Function, class... Args > void call_once( std::once_flag& flag, Function&& f, Args&& args... ); |
(seit C++11) | |
Führt die Funktion
f
genau einmal, auch wenn von mehreren Threads aufgerufen . 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.
Jede Gruppe von
call_once
Anrufungen, die den gleichen std::once_flag Objekt empfängt, wird die folgenden Anforderungen erfüllen: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.
- Genau eine Ausführung genau eine der Funktionen (übergeben als
f
den Anrufungen in der Gruppe) durchgeführt. Es ist nicht definiert, welche Funktion für die Ausführung ausgewählt werden. Die ausgewählte Funktion läuft im selben Thread wie diecall_once
Anrufung ging sie an wurde .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.
- Kein Aufruf in der Gruppe kehrt vor dem genannten Ausführung der ausgewählten Funktion erfolgreich abgeschlossen ist, das heißt, nicht über eine Ausnahme zu verlassen .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.
- Wenn die ausgewählte Funktion tritt über Ausnahme, wird es dem Anrufer propagiert. Eine weitere Funktion wird dann ausgewählt und ausgeführt .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.
Inhaltsverzeichnis |
[Bearbeiten] Parameter
flag | - | Eine Aufgabe, für die genau eine Funktion ausgeführt
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 | - | funktionieren zu nennen
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... | - | Argumente, die an die Funktion übergeben
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. |
[Bearbeiten] Rückgabewert
(None)
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.
[Bearbeiten] Ausnahmen
- std::system_error, wenn Ihr Zustand Anrufe verhindert
call_once
von der Ausführung, wie angegebenOriginal: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. - jede Ausnahme von
f
geworfenOriginal: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.
[Bearbeiten] Beispiel
#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(); }
Output:
Called once
[Bearbeiten] Siehe auch
(C++11) |
Hilfsobjekt um sicherzustellen, dass call_once die Funktion aufruft nur einmal 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. (Klasse) |