问一个多线程基础问题
int index=0;
int tickets=100;
HANDLE hMutex;
DWORD WINAPI FunProc1(LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
cout<<"Thread1 is telling ticket:"<<tickets--<<endl;
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI FunProc2(LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
cout<<"Thread2 is telling ticket:"<<tickets--<<endl;
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
void main()
{
HANDLE hThread1,hThread2;
hThread1=CreateThread(NULL,0,FunProc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,FunProc2,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
hMutex=CreateMutex(NULL,FALSE,"tickets");
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
}1 if(hMutex)这是在判断什么?
2if(ERROR_ALREADY_EXISTS==GetLastError())这是在判断什么?
3CloseHandle(hThread1);表示什么?
4WaitForSingleObject(hMutex,INFINITE) 主线程为什么还有写这个?
5两次"ReleaseMutex(hMutex);"分别释放的什么?
...全文
相关推荐
一个多线程的COM程序
**多线程基础** 在多线程编程中,多个线程共享同一进程的资源,可以并发执行任务,提高程序响应速度。
使用threading建立了一个多线程的基础程序
使用threading建立了一个多线程的基础程序使用threading建立了一个多线程的基础程序使用threading建立了一个多线程的基础程序使用threading建立了一个多线程的基础程序使用th
今天没事给大家写一个多线程例子
今天没事给大家写一个多线程例子今天没事给大家写一个多线程例子
使用三种VC的多线程同步方法编写一个多线程的程序
1.使用三种VC的多线程同步方法编写一个多线程的程序(要求在屏幕上先显示Hello,再显示World)。1)基于全局变量的多线程同步程序;2)基于事件的多线程同步程序;3)基于临界区的多线程同步程序。
一个多线程示例程序及多线程常见问题介绍
C++Builder是一款强大的集成开发环境(IDE),它支持C++语言,为开发者提供了创建多线程应用的能力。在这个主题中,我们将深入探讨一个多线程示例程序以及在使用多线程时可能遇到的常见问题。


进程/线程/DLL
