📄 chauffeur_conductor.cpp
字号:
// Chauffeur_Conductor.cpp : Defines the entry point for the console application.
//
/********************************************************************
created: 2005/11/07
created: 7:11:2005 13:57
filename: e:\vc\vc workspace\chauffeur_conductor\chauffeur_conductor.cpp
file path: e:\vc\vc workspace\chauffeur_conductor
file base: chauffeur_conductor
file ext: cpp
author: T323-3-25 陈锋
purpose: 司机售票员同步问题
*********************************************************************/
#include "stdafx.h"
#include "Chauffeur_Conductor.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
//HANDLE s1,s2;
HANDLE s1=CreateSemaphore(NULL,0,2,"chauffeur");
HANDLE s2=CreateSemaphore(NULL,0,2,"conductor");
using namespace std;
DWORD chauffeur(void*pD)
{
for (int i=0;i<5;i++)
{
WaitForSingleObject(s1,INFINITE);
Sleep(1000);
printf("启动车辆!\n");
Sleep(1000);
printf("\t正常行车!\n");
Sleep(1000);
printf("\t\t到站停车!\n");
ReleaseSemaphore(s2,1,NULL);
}
return 0;
}
DWORD conductor(void*pD)
{
for (int i=0;i<5;i++)
{
Sleep(1000);
printf("\n关车门!\n");
ReleaseSemaphore(s1,1,NULL);
Sleep(1000);
printf("\t售票!\n");
WaitForSingleObject(s2,INFINITE);
Sleep(1000);
printf("\t\t开车门!\n");
}
return 0;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
HANDLE hThread[2];
//创建进程
CWinThread* pT1=AfxBeginThread((AFX_THREADPROC)chauffeur,(void*)1);
CWinThread* pT2=AfxBeginThread((AFX_THREADPROC)conductor,(void*)2);
hThread[0]=pT1->m_hThread;
hThread[1]=pT2->m_hThread;
//等待线程结束
WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
//关闭句柄
CloseHandle(s1);
CloseHandle(s2);
}
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -