C++线程中程序无法继续,停在while()
变量stop 在主程序中通过事件可以变为1。意图是stop==1 的时候线程可以开始执行线程的任务。
奇怪的是while{}中如果有cout这行,程序进行的没有问题;
如果注释掉,程序就停在while了,也不知道停在哪儿,为什么停。输出只有:
Enter thread stop = 0
before
代码:
void MySocket::recvProc( MySocket *p_Socket) // 线程函数;静态成员函数。传入的参数为this
{
cout<<"Enter thread "<<" stop = "<<p_Socket->stop<<endl;
cout<<"before"<<endl;
while(p_Socket->stop!=1)
{
// cout<<""<<endl;
}
cout<<"after"<<endl;
cout<<"at thread "<<" stop = "<<p_Socket->stop<<endl;
...
}
有没有人遇到过类似的情况啊?或者知道其中的奥妙
万分感谢
[解决办法]
帮顶,话说stop是什么事件值会修改
[解决办法]
像你这种情况不加锁也可以的,你这就是因为p_Socket->stop值一直没有变为1