|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 隐袭战龙 于 2014-4-7 11:03 编辑
同样是在大炮打飞机里面
这是一个多线程版本
里面要同时输出飞机,子弹以及信息
但是输出的时候产生了位置冲突(由于是多线程,输出的时候跑到了不正确的输出位置,互相冲突)
我便加了锁,如下- unsigned int __stdcall MoveProcess(LPVOID game)
- {
- while(repeat)
- {
- while(OutputLock);
- OutputLock=true;
- planemove();
- makeplane();
- printplane();
- OutputLock=false;
- Sleep(speed);
- }
- _endthreadex(0);
- return 0;
- }
- unsigned int __stdcall InfoProcess(LPVOID game)
- {
- while(repeat)
- {
- while(OutputLock);
- OutputLock=true;
- printinfo();
- OutputLock=false;
- }
- _endthreadex(0);
- return 0;
- }
复制代码
本以为这样可以解决问题,但是出现了类似于死循环的症状就是在while(OutputLock);这里卡住,要卡上一段时间
我想问的是怎么样才能正确加锁?
|
|