How to execute a piece of code only once?

后端 未结 8 845
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 04:35

I have an application which has several functions in it. Each function can be called many times based on user input. However I need to execute a small segment of the code wi

8条回答
  •  悲&欢浪女
    2020-12-01 05:17

    Using C++11 -- use the std::call_once

    #include 
    
    std::once_flag onceFlag;
    
    {
        ....
        std::call_once ( onceFlag, [ ]{ /* my code body here runs only once */ } );
        ....
    }
    

提交回复
热议问题