关于vc多线程的一个程序
答案:1 悬赏:80 手机版
解决时间 2021-04-06 07:13
- 提问者网友:
- 2021-04-05 11:23
关于vc多线程的一个程序
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-04-05 11:41
#include
#include
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!"< hMutex=CreateMutex(NULL,FALSE,NULL); //初始化线程互斥对象
while(1)
{
WaitForSingleObject(hMutex,INFINITE); //等待互斥对象
if(tickets>0)//如果车票数量大于0则卖票,车票数量减一
{
cout<<"main thread sell tickets:"< tickets--;
}
else
break; //若车票卖光了,则跳出while循环
ReleaseMutex(hMutex); //主线程卖出一张票后,释放线程互斥对象
}
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter) //定义线程1的处理函数
{
cout<<" thread1 is running!"< while(1)
{
WaitForSingleObject(hMutex,INFINITE); //等待线程互斥对象
if(tickets>0) //若车票没卖完,则卖票,车票数减一
{
cout<<"thread1 sell tickets:"< tickets--;
}
else
break; //否则跳出while循环
ReleaseMutex(hMutex); //卖票成功,释放线程互斥对象
}
return 0;
}
#include
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!"<
while(1)
{
WaitForSingleObject(hMutex,INFINITE); //等待互斥对象
if(tickets>0)//如果车票数量大于0则卖票,车票数量减一
{
cout<<"main thread sell tickets:"<
}
else
break; //若车票卖光了,则跳出while循环
ReleaseMutex(hMutex); //主线程卖出一张票后,释放线程互斥对象
}
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter) //定义线程1的处理函数
{
cout<<" thread1 is running!"<
{
WaitForSingleObject(hMutex,INFINITE); //等待线程互斥对象
if(tickets>0) //若车票没卖完,则卖票,车票数减一
{
cout<<"thread1 sell tickets:"<
}
else
break; //否则跳出while循环
ReleaseMutex(hMutex); //卖票成功,释放线程互斥对象
}
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯