gpt4 book ai didi

C++ - 不断递增一个整数

转载 作者:行者123 更新时间:2023-11-28 00:15:22 25 4
gpt4 key购买 nike

我正在寻找一种让整数每 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com