请高手帮我分析一下这个多线程的小练习程序。
这个是我按照孙鑫C++视频里面的练习做的 我得编程环境是vs2010+win7
代码如下:
- C/C++ code
#include<iostream>#include <windows.h>using namespace std;DWORD WINAPI GThreadProc1( __in LPVOID lpParameter );DWORD WINAPI GThreadProc2( __in LPVOID lpParameter );int tickets=100;HANDLE hMutex;int main(){ HANDLE hThread1; HANDLE hThread2; hThread1=CreateThread(NULL,0,GThreadProc1,NULL,0,NULL); hThread2=CreateThread(NULL,0,GThreadProc2,NULL,0,NULL); Sleep(5000); hMutex=CreateMutex(NULL,false,NULL); return 0;}DWORD WINAPI GThreadProc1( __in LPVOID lpParameter ){ WaitForSingleObject(hMutex,INFINITE); while (true) { Sleep(1); if (tickets>0) { cout<<"No.1 seller sells ticket :"<<tickets--<<endl; } ReleaseMutex(hMutex); } return 0;}DWORD WINAPI GThreadProc2( __in LPVOID lpParameter ){ WaitForSingleObject(hMutex,INFINITE); while (true) { Sleep(1); if (tickets>0) { cout<<"No.2 seller sells ticket :"<<tickets--<<endl; ReleaseMutex(hMutex); } } return 0;}
为什么会出现如下图所示的运行结果:
如果我把代码中两个线程的sleep(1)去掉又会出现如下两图所示的情况
1
2
我的理想状况是让他们按照顺序卖票。请问出现以上情况的原理原因是什么啊?希望高手多多帮助。
[解决办法]
多线程互斥量
sleep在互斥量的保护范围之内
不知道你是否是把两个sleep同时去除的呢??
[解决办法]
#include<iostream>
#include <windows.h>
using namespace std;
DWORD WINAPI GThreadProc1(
__in LPVOID lpParameter
);
DWORD WINAPI GThreadProc2(
__in LPVOID lpParameter
);
int tickets=100;
HANDLE hMutex;
int main()
{
HANDLE hThread1;
HANDLE hThread2;
hMutex=CreateMutex(NULL,false,NULL);
hThread1=CreateThread(NULL,0,GThreadProc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,GThreadProc2,NULL,0,NULL);
Sleep(5000);
system("pause");
return 0;
}
DWORD WINAPI GThreadProc1(
__in LPVOID lpParameter
)
{
while (true)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
cout<<"No.1 seller sells ticket :"<<tickets--<<endl;
}
else
{
break;
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI GThreadProc2(
__in LPVOID lpParameter
)
{
while (true)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
cout<<"No.2 seller sells ticket :"<<tickets--<<endl;
ReleaseMutex(hMutex);
}
else
{
break;
}
}
return 0;
}
[解决办法]
你线程2的ReleaseMutex(hMutex)语句写错了吧;
怎么写到if语句里面啊。。要是不满足条件,。。。那不是就执行不了ReleaseMutex(hMutex);
所以会出现那种情况
[解决办法]
上面没贴好
- C/C++ code
#include<iostream>#include <windows.h>using namespace std;DWORD WINAPI GThreadProc1( LPVOID lpParameter );DWORD WINAPI GThreadProc2( LPVOID lpParameter );int tickets=100;//HANDLE hMutex;CRITICAL_SECTION g_cs;int main(){ HANDLE hThread1; HANDLE hThread2; InitializeCriticalSection(&g_cs); //hMutex=CreateMutex(NULL,false,NULL); hThread1=CreateThread(NULL,0,GThreadProc1,NULL,0,NULL); hThread2=CreateThread(NULL,0,GThreadProc2,NULL,0,NULL); Sleep(5000); CloseHandle(hThread1); CloseHandle(hThread2); DeleteCriticalSection(&g_cs); return 0;}DWORD WINAPI GThreadProc1( LPVOID lpParameter ){ //WaitForSingleObject(hMutex,INFINITE); while (true) { ::EnterCriticalSection(&g_cs); Sleep(1); if (tickets>0) { cout<<"No.1 seller sells ticket :"<<tickets--<<endl; } ::LeaveCriticalSection(&g_cs); //ReleaseMutex(hMutex); } return 0;}DWORD WINAPI GThreadProc2( LPVOID lpParameter ){ //WaitForSingleObject(hMutex,INFINITE); while (true) { ::EnterCriticalSection(&g_cs); Sleep(1); if (tickets>0) { cout<<"No.2 seller sells ticket :"<<tickets--<<endl; //ReleaseMutex(hMutex); } ::LeaveCriticalSection(&g_cs); } return 0;}