call_once/once_flag的使用
生活随笔
收集整理的這篇文章主要介紹了
call_once/once_flag的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
call_once/once_flag的使用
保證在多線程環境中某個函數僅僅被調用一次,可以使用std::call_once函數,并且需要一個入參once_flag類型的入參
#include<iostream> #include <string> #include <tuple> #include <mutex> #include <thread> #include <list> #include <condition_variable> using namespace std;std::once_flag flag;void only_do_once() {std::call_once(flag, []() {std::cout << "只調用一次哦"; });}int main() {std::thread t1(only_do_once); std::thread t2(only_do_once); std::thread t3(only_do_once);t1.join();t2.join();t3.join(); }總結
以上是生活随笔為你收集整理的call_once/once_flag的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kali字典_kali黑客系统wpsca
- 下一篇: 武汉理工大学计算机专业课程表,武汉理工大