gpt4 book ai didi

c++ - 如何使用线程来打破主C++中的循环

转载 作者:行者123 更新时间:2023-11-30 01:57:27 25 4
gpt4 key购买 nike

我在 C++ 中使用以下线程来检查是否满足特定条件,如果满足则应该中断循环。我在 while 循环中调用线程,所以我需要它中断。刷新 token 由另一个线程更新。

void ThreadCheck( void* pParams )
{
if(refresh)
{
continue;
}
}

我的 while 循环:-

while(crun)
{
refresh = false;
_beginthread( ThreadCheck, 0, NULL );
rlutil::setColor(8);
cout<<"Send>> ";
getline(cin, msg); //Make a custom function of this.
if(stricmp(msg.c_str(), "exit")==0)
{
crun = false;
}
else if(msg.empty() || stricmp(msg.c_str()," ")==0)
{
rlutil::setColor(4);
cout<<"Plz enter a valid message!\n";

continue;

} else {
manager('c', msg);
// msg.append("\n");
// chat_out<<msg;
// chat_out.close();
}
cout<<"\n";
}

最佳答案

您不能修改一个线程中的值,而另一个线程正在或可能正在访问它。您需要使用某种形式的同步,例如锁。

关于c++ - 如何使用线程来打破主C++中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18694351/

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