大家帮我看一下这个是哪里错了
#include <windows.h>#include <process.h>
#include <iostream.h>
HANDLE g_putApple;
HANDLE g_putBanana;
HANDLE g_eatApple;
HANDLE g_eatBanana;
HANDLE g_dish;
HANDLE m_hmtx;
unsigned long _beginthreadex(
void *security, //安全属性,NULL为默认安全属性
unsigned stack_size, //指定线程堆栈的大小。如果为0,则线程堆栈大小和创建它的线程的相同。一般用0
unsigned ( __stdcall *start_address )( void * ), //指定线程函数的地址,也就是线程调用执行的函数地址(用函数名称即可,函数名称就表示地址)
void *arglist,// 传递给线程的参数的指针,可以通过传入对象的指针,在线程函数中再转化为对应类的指针
unsigned initflag, //线程初始状态,0:立即运行;CREATE_SUSPEND:suspended(悬挂)
unsigned *thrdaddr );//用于记录线程ID的地址
UINT WINAPI PutAppleThread(PVOID pvParam)
{
for(int i = 0; i < 10; i++)
{
WaitForSingleObject(g_dish, INFINITE);
WaitForSingleObject(g_putApple, INFINITE);
cout << "put apple!/n";
SetEvent(g_eatApple);
Sleep(5);
}
return 0;
}
//放香蕉
UINT WINAPI PutBananaThread(PVOID pvParam)
{
for(int i = 0; i < 10; i++)
{
WaitForSingleObject(g_dish,INFINITE);
WaitForSingleObject(g_putBanana, INFINITE);
cout << "put banana!/n";
SetEvent(g_eatBanana);
}
return 0;
}
//吃苹果
UINT WINAPI EatAppleThread(PVOID pvParam)
{
for(int i = 0; i < 10; i++)
{
WaitForSingleObject(g_eatApple, INFINITE);
cout << "eat apple!/n";
SetEvent(g_putApple);
SetEvent(g_dish);;
}
return 0;
}
//吃香蕉
UINT WINAPI EatBananaThread(PVOID pvParam)
{
for(int i = 0; i < 10; i++)
{
WaitForSingleObject(g_eatBanana, INFINITE);
cout << "eat banana!/n";
SetEvent(g_putBanana);
SetEvent(g_dish);
Sleep(6);
}
return 0;
}
int main()
{
g_putApple = CreateEvent(NULL, FALSE, TRUE, NULL);
g_putBanana = CreateEvent(NULL, FALSE, TRUE, NULL);
g_eatApple = CreateEvent(NULL, FALSE, FALSE, NULL);
g_eatBanana = CreateEvent(NULL, FALSE, FALSE, NULL);
g_dish = CreateEvent(NULL, FALSE, TRUE, NULL);
m_hmtx = CreateMutex(NULL, FALSE, NULL);
HANDLE hThread[4];
int x;
hThread[0] = (HANDLE)_beginthreadex(NULL, 0, PutAppleThread, (void *)&x, 0, NULL);
hThread[1] = (HANDLE)_beginthreadex(NULL, 0, PutBananaThread, (void *)&x, 0, NULL);
hThread[2] = (HANDLE)_beginthreadex(NULL, 0, EatAppleThread, (void *)&x, 0, NULL);
hThread[3] = (HANDLE)_beginthreadex(NULL, 0, EatBananaThread, (void *)&x, 0, NULL);
char ch;
cin >> ch;
return 0;
}