c++多线程问题
求大神,请解释下列代码,或者另说一下创建多线程的方法//这是2个线程模拟卖火车票的小程序#include<windows.h>#include<iostream.h>DW...
求大神,请解释下列代码,或者另说一下创建多线程的方法
//这是2个线程模拟卖火车票的小程序
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data
int index=0;
int ticket=10;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
//创建线程
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
//创建互斥对象
hMutex=CreateMutex(NULL,TRUE,"ticket");
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的入口函数
DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (ticket>0)
{
Sleep(1);
cout<<"thread1 sell ticket :"<<ticket--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
//线程2的入口函数
DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (ticket>0)
{
Sleep(1);
cout<<"thread2 sell ticket :"<<ticket--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
} 展开
//这是2个线程模拟卖火车票的小程序
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data
int index=0;
int ticket=10;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
//创建线程
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
//创建互斥对象
hMutex=CreateMutex(NULL,TRUE,"ticket");
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的入口函数
DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (ticket>0)
{
Sleep(1);
cout<<"thread1 sell ticket :"<<ticket--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
//线程2的入口函数
DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (ticket>0)
{
Sleep(1);
cout<<"thread2 sell ticket :"<<ticket--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
} 展开
2个回答
展开全部
你不觉得你的两个线程是一样的,你只需要写一个,然后这样调用
HANDLE hThread1;
HANDLE hThread2;
//创建线程
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
这两个线程是跑在不同空间,使用相同的代码空间。
创建线程有很多方式,但是本质都是调用
unsigned long _beginthread( void( __cdecl *start_address )(
void * ), unsigned stack_size, void *arglist
);
unsigned long _beginthreadex( void *security, unsigned
stack_size, unsigned ( __stdcall *start_address )(
void * ), void *arglist, unsigned initflag, unsigned
*thrdaddr );
HANDLE hThread1;
HANDLE hThread2;
//创建线程
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
这两个线程是跑在不同空间,使用相同的代码空间。
创建线程有很多方式,但是本质都是调用
unsigned long _beginthread( void( __cdecl *start_address )(
void * ), unsigned stack_size, void *arglist
);
unsigned long _beginthreadex( void *security, unsigned
stack_size, unsigned ( __stdcall *start_address )(
void * ), void *arglist, unsigned initflag, unsigned
*thrdaddr );
追问
本人现在对C++的多线程创建方法一点不懂,求解释(感觉java相对于c++在这方面太简单了)
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |