我正在寻找一种让整数每 10 秒左右不断递增的方法。我知道如何让整数递增,但我不知道如何让它继续递增,而不管程序的其余部分当前发生了什么。
最佳答案
为此使用 std::thread
。
创建一个函数
void incrementThread(int &i)
{
while(someCondition)
{
//sleep for 10 seconds
//increment your value
i++;
std::this_thread::sleep_for(std::chrono::duration<int>(10));
}
}
现在从 main
开始:
int main()
{
int i = 0;
std::thread t(incrementThread, std::ref(i));
t.detach() // or t.join()
}
关于C++ - 不断递增一个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665958/