关于vc多线程的一个程序

#include<windows.h>#include<iostream.h>DWORDWINAPIFun1Proc(LPVOIDlpParameter);inttick... #include<windows.h>
#include<iostream.h>

DWORD WINAPI Fun1Proc(LPVOID lpParameter);

int tickets=100;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
cout<<"main thread is running!"<<endl;
hMutex=CreateMutex(NULL,FALSE,NULL);
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
cout<<"main thread sell tickets:"<<tickets<<endl;
tickets--;
}

else
break;
ReleaseMutex(hMutex);
}
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
cout<<" thread1 is running!"<<endl;
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{

cout<<"thread1 sell tickets:"<<tickets<<endl;
tickets--;
}

else
break;
ReleaseMutex(hMutex);
}
return 0;
}
上面的代码什么意思?能说的具体点不?
展开
 我来答
灵_火
2008-12-01 · TA获得超过178个赞
知道答主
回答量:121
采纳率:0%
帮助的人:98.8万
展开全部
#include<windows.h>
#include<iostream.h>

DWORD WINAPI Fun1Proc(LPVOID lpParameter); //声明线程函数1

int tickets=100; //初始化车票的数量
HANDLE hMutex; //定义线程互斥对象
void main()
{
HANDLE hThread1; //定义线程hThread1
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL); //创建线程hThread1
CloseHandle(hThread1); //关闭线程句柄,
cout<<"main thread is running!"<<endl;
hMutex=CreateMutex(NULL,FALSE,NULL); //初始化线程互斥对象
while(1)
{
WaitForSingleObject(hMutex,INFINITE); //等待互斥对象
if(tickets>0)//如果车票数量大于0则卖票,车票数量减一
{
cout<<"main thread sell tickets:"<<tickets<<endl;
tickets--;
}

else
break; //若车票卖光了,则跳出while循环

ReleaseMutex(hMutex); //主线程卖出一张票后,释放线程互斥对象
}
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter) //定义线程1的处理函数
{
cout<<" thread1 is running!"<<endl;
while(1)
{
WaitForSingleObject(hMutex,INFINITE); //等待线程互斥对象
if(tickets>0) //若车票没卖完,则卖票,车票数减一
{

cout<<"thread1 sell tickets:"<<tickets<<endl;
tickets--;
}

else
break; //否则跳出while循环
ReleaseMutex(hMutex); //卖票成功,释放线程互斥对象
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式