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. |
| Заголовочный файл <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 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. - любое исключение по
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.
[править] Пример
#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(); }
Вывод:
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. (класс) |