多线程的问题,同时创建主函数肯定比其它线程抢先吗?
#include<iostream>
#include<windows.h>
using namespace std;
HANDLE hMutex;
DWORD TEMP=1000;
DWORD WINAPI Xfun(LPVOID LpParameter)
{
while(1)
{
WaitForSingleObject(hMutex,INFINITE); // 申请资源
cout<<"XFun display:"<<TEMP--<<endl;
Sleep(10000);
ReleaseMutex(hMutex); // 释放申请的资源
}
}
DWORD WINAPI Fun(LPVOID lpParameter) // 线程函数
{
HANDLE hThread = CreateThread(NULL,0,Xfun,NULL,0,NULL); // 创建一个线程
hMutex = CreateMutex(NULL,FALSE,"screen");
while(1)
{
WaitForSingleObject(hMutex,INFINITE); // 申请资源
cout<<"Fun display:"<<TEMP--<<endl;
Sleep(10000);
ReleaseMutex(hMutex); // 释放申请的资源
}
}
int main()
{
HANDLE hThread = CreateThread(NULL,0,Fun,NULL,0,NULL); // 创建一个线程
hMutex = CreateMutex(NULL,FALSE,"screen");//创建一个独占资源
CloseHandle(hThread);
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
cout<<"main display:"<<TEMP--<<endl;
Sleep(2000);
ReleaseMutex(hMutex);
}
return 0;
}