www.pudn.com > ThreadCompete.rar > ThreadCollection.h


#ifndef _COLLECT_PERSON_THREAD_H_ 
#define _COLLECT_PERSON_THREAD_H_ 
 
#include  
#include  
#include  
 
#define   WM_USER_NOTIFY  WM_USER+300 
#define   PI            3.14159 
#define   PI2           2*PI 
 
#define   KNIFE_BIT     1 
#define   FORK_BIT      2 
 
#define   KNIFE_MASK    2 
#define   FORK_MASK     1 
 
#define   BLOCK         0 
#define   CATCHING      1 
#define   BEGINEATING   2 
#define   FINISHDOWN    3 
#define   TIME_BASE     1 
 
int  GetRandomInt( bool bInit, int limit ); 
 
class PERSON_INFO : public CObject 
{ 
public: 
	friend class PersonThread; 
	PersonThread * pthread;			//该人所对应的线程对象 
	DWORD   defaultpriority;		//缺省随机优先级,非缺省时所有线程设置同样优先级 
	int   	index;					//人的位置序号:仅考虑拿到餐具,没考虑从哪边拿到.... 
	int	    tools, fork,knife;		//取得的标志与位置序号 
	int		status, step, steptime; 
	struct  compete{ int loc,what; }compete; 
	CString message; 
}; 
typedef CTypedPtrArray CPersonInfoArray;	//路口集合 
 
 
class CThreadCollection //管理所有的线程集合 
{ 
public: 
	bool             bUnBlockSelf; 
	bool			 bUpdateNotify;     //允许显示输出 
	bool			 bThreadsPause;		//标志 所有线程 suspend 或 resume 状态 
	bool             fSyncChecked;		//CMutex的同步互斥开关 
	bool             fBlockChecked;		//不阻碍邻居开关 
	CMutex			 m_mutex;			//同步互斥 
	CPersonInfoArray personArr;			//人与线程的动态矩阵数组 
	CUIntArray       dinnerToolArr;		//餐具的动态矩阵数组 
	CUIntArray		 threadsResumeArr;	//激活线程集的随机顺序组 
	CPoint point;    int rr,delta, dd;	//坐标定义 
    CWnd*            m_pWnd;			//显示窗体 
	CListBox*        m_pListBox;		//文本显示列表 
	int				 m_step, nContinousPrompt; 
    CMapWordToPtr    competeMap; 
    long   counter; 
 
	CThreadCollection(); 
	virtual ~CThreadCollection(); 
 
	int   GetItemsUpper(); 
	void  SetSyncCheckEnable( bool check ){	fSyncChecked = check; } 
	void  SetBlockCheckEnable(bool check){ fBlockChecked=check;} 
	void  SetUnBlockSelfCheck(bool check){ bUnBlockSelf =check;} 
	void  SetupThreadStepTime( int delay ); 
	void  SetDisplayCoordinate(CPoint *pt, int ra, int delta); 
	void  CreateRandomMap(CUIntArray &Arr, int nums); 
	void  initThreadCollectionClass(CWnd* pWnd, CListBox*pBox, int nums, int delay); 
	PersonThread* CreateBeginThread(PERSON_INFO * person, DWORD nPriority); 
	void  initCollectionThreads(int nCurSel); 
	void  DestroyThreadCollection(); 
	void  FastKillPersonThreads(); 
	void  SlowKillPersonThreads(); 
	int   FinishPersonThreads(); 
 
	void  ToggleCollectionThreadsPause(bool bPaused); 
	void  SetCurrentPriorityClass(int nCurSel); 
	int   CheckPersonCatchStatus(int status); 
	int   CheckPersonFinishStatus(); 
	DWORD SetPersonThreadsPriority(int nCurSel); 
 
	void  StatCompetePersons(); 
	void  PromptContinousCheckBox(int show); 
	int   WillBlockNeighbor(int iCurrent, int kf, int upper); 
	int   CatchDinnerToolkit(PERSON_INFO * person, bool fromLeft, bool isFork); 
	void  PutdownDinnerToolkit(PERSON_INFO * person, int set, int mask); 
	void  UpdateNotify( PERSON_INFO * person ); 
	void  DrawCollectionResult(CDC *pDC); 
}; 
 
// PersonThread thread 
 
class PersonThread : public CWinThread 
{ 
	DECLARE_DYNCREATE(PersonThread) 
public:	//通用定义,根据所加变量重新虚构函数 
	HANDLE m_hEventKill, m_hEventDead;		//被杀死亡消息, 如采用自动结束方式无需这些变量 
	static HANDLE m_hAnotherDead;			//线程退出执行体end -- delete 中自动死亡消息 
	static CRITICAL_SECTION m_criLock;		//同步互斥 
 
	virtual BOOL InitInstance();	 //执行体的一种 
	virtual int  Run();				//执行体的一种 
	        void KillThread();		//kill主要使它脱离执行体 
	virtual void Delete();			//在退出线程执行体后调用,可以设置dead事件 
	virtual ~PersonThread();		//自动析构 
	PersonThread(){;}				//自动建构 
 
public:	//专用定义,属于本对象功能定义 
	friend class PERSON_INFO; 
	PERSON_INFO*		person;		 //就餐者信息 
	CThreadCollection * m_pThreads;  //线程集合 
	BOOL         m_bDone;			 //线程完成退出 
 
	void  ThreadSingleStepRun(); 
	void  initPersonThread(CThreadCollection *threads, PERSON_INFO*pPerson); 
 	int   GetPersonStatus(){ return person->status;} 
	int   CheckOneToolkit(int index, int kf, int upper); 
	int   CheckPersonStatus( int &status ); 
    int   CatchDinnerToolkit(); 
	void  DelayStepTime(BOOL bDone); 
	void  PutdownDinnerToolkit( ); 
 
	DECLARE_MESSAGE_MAP() 
}; 
 
#endif