#include
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID IpParameter);
DWORD WINAPI Fun2Proc(LPVOID IpParameter);
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hMutex = CreateMutex(NULL, FALSE, NULL);
hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2 = CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
cout<<"main:running"<
}
DWORD WINAPI Fun1Proc(LPVOID IpParameter)
{
WaitForSingleObject(hMutex,INFINITE);
cout<<"fun1:a"<
return 0;
}
DWORD WINAPI Fun2Proc(LPVOID IpParameter)
{
WaitForSingleObject(hMutex,INFINITE);
cout<<"fun2:b"<
return 0;
}
为何主线程有时会输出两次?主线程的运行机制是怎样的?