std::call_once
来自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. |
| Defined in header <mutex>
|
||
| template< class Function, class... Args > void call_once( std::once_flag& flag, Function&& f, Args&& args... ); |
(C++11 起) | |
。執行功能
f一次,即使從多個線程調用. 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.
。每個組的
call_once調用接收相同的std::once_flag對象的將符合下列要求:。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.
- 。 整整一個執行完全相同的功能(通過
f該組中的調用)。將被選擇用於執行的函數,它是未定義。所選擇的功能call_once調用傳遞給運行在同一個線程中.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.- 。沒有調用成功完成之前,上述執行所選擇的功能組中的回報,也就是說,不通過異常退出.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.
- 。如果所選的功能異常退出,它會傳播給調用者。然後,選擇和執行的另一項功能.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.
目錄
[编辑] 。參數。
flag - 。一個對象,其中一個功能被執行。Original:an object, for which exactly one function gets executedThe 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 callThe 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 functionThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.===。 返回值。===
。 (無)。Original:(none)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.[编辑] 。例外。
- 。 std::system_error任何條件阻止
call_once執行所指定的調用。Original: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. - 。
f拋出的任何異常。Original: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.
[编辑] 。為例。
#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
[编辑] 。另請參閱。
(C++11)call_once的輔助對象,以確保調用該函數一次Original:helper object to ensure that call_once invokes the function only onceThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(類) -