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.
- まさに機能のちょうど1つの(グループ内の呼び出しに
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 | - | 正確に1つの関数が実行されているオブジェクト、
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 | - | 呼び出される関数です
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... | - | 引数は、関数に渡す
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. |
[編集] 値を返します
(なし)
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.
[編集] 例外
- 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 once The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (クラス) |