无限循环中每60秒重置变量值每60秒



我正在进行一个项目,该项目需要重置每个1 Minute的变量而不暂停infinite loop

例如:

int temp = 0;
while(true){
temp ++;
//After 60 Seconds
call_to_a_function(temp);
temp = 0;
}

如何在C++中实现此目标?

您的帮助非常感谢!

    atomic_int  temp;
    std::thread t( [&temp]()
                  {
                      while( temp!= -1){
                          std::this_thread::sleep_for(std::chrono::seconds(60));
                          temp=0;
                      }
                  });

    while ( true )
    {
        temp < INT_MAX ? temp++ : 0;
        cout << temp << endl;
    }
    // do some stuff
    // when program needs to exit, do this to avoid a crash
    temp = -1; // t will exit the loop
    t.join();  // wait until t finishes running

虽然有睡眠的循环可能会被优化。

最新更新