⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servicethread.h

📁 游戏框架
💻 H
字号:
// ***************************************************************
//  ServiceThread   version:  1.0
//  -------------------------------------------------------------
//	File Name:	ServiceThread.h
//	Created:	2007/07/18
//	Modified:	2007/07/18   16:40
//	Author:		William.Liang
//  Msn:		lwq49@msn.com
//  Email:		lwq49@21cn.com, lwq49@msn.com
//	Description:
//
//	Purpose:	
//  -------------------------------------------------------------
//  license:
//
//  The contents of this file are subject to the Mozilla Public
//  License Version 1.1 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy
//  of the License at http://www.mozilla.org/MPL/ Software dis-
//  tributed under the License is distributed on an "AS IS" 
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or im-
//  plied. See the License for the specific language governing
//  rights and limitations under the License.
//
//  The Initial Developer of the Original Code is William.Liang .
//  Copyright (C) 2007 - All Rights Reserved.
// ***************************************************************
#ifndef SERVICE_THREAD_HEAD_FILE
#define SERVICE_THREAD_HEAD_FILE

#pragma once

#include "ComService.h"

//////////////////////////////////////////////////////////////////////////
//接口定义

//同步对象接口
interface IThreadLock
{
	//锁定函数
	virtual void Lock()=NULL;
	//解锁函数 
	virtual void UnLock()=NULL;
};

//线程接口
interface IServiceThread
{
	//状态判断
	virtual bool IsRuning()=NULL;
	//启动线程
	virtual bool StartThead()=NULL;
	//停止线程
	virtual bool StopThread(DWORD dwWaitSeconds)=NULL;
	//中止线程
	virtual bool TerminateThread(DWORD dwExitCode)=NULL;
};

//////////////////////////////////////////////////////////////////////////

//临界区同步类
class COM_SERVICE_CLASS CThreadLock : public IThreadLock
{
	//变量定义
private:
	CRITICAL_SECTION					m_csLock;					//临界变量

	//函数定义
public:
	//构造函数
	inline CThreadLock() { ::InitializeCriticalSection(&m_csLock); }
	//析构函数
	inline ~CThreadLock() { ::DeleteCriticalSection(&m_csLock); }

	//功能函数
public:
	//锁定函数
	virtual inline void Lock() { ::EnterCriticalSection(&m_csLock); }
	//解锁函数 
	virtual inline void UnLock() { ::LeaveCriticalSection(&m_csLock); }
};

//////////////////////////////////////////////////////////////////////////

//安全同步锁定句柄
class COM_SERVICE_CLASS CThreadLockHandle
{
	//变量定义
private:
	int								m_nLockCount;				//锁定计数
	IThreadLock						* m_pIThreadLock;			//锁定对象

	//函数定义
public:
	//构造函数
	CThreadLockHandle(IThreadLock * pIThreadLock, bool bAutoLock=true);
	//析构函数
	virtual ~CThreadLockHandle();

	//功能函数
public:
	//锁定函数
	void Lock();
	//解锁函数 
	void UnLock();
	//获取锁定次数
	int inline GetLockCount() { return m_nLockCount; }
};

//////////////////////////////////////////////////////////////////////////

//线程对象类
class COM_SERVICE_CLASS CServiceThread : public IServiceThread
{
	//变量定义
private:
	volatile bool						m_bRun;							//运行标志
	UINT								m_uThreadID;					//线程标识
	HANDLE								m_hThreadHandle;				//线程句柄

	//函数定义
protected:
	//构造函数
	CServiceThread(void);
	//析构函数
	virtual ~CServiceThread(void);

	//接口函数
public:
	//获取状态
	virtual bool IsRuning();
	//启动线程
	virtual bool StartThead();
	//停止线程
	virtual bool StopThread(DWORD dwWaitSeconds=INFINITE);
	//中止线程
	virtual bool TerminateThread(DWORD dwExitCode);

	//功能函数
public:
	//获取标识
	UINT GetThreadID() { return m_uThreadID; }
	//获取句柄
	HANDLE GetThreadHandle() { return m_hThreadHandle; }
	//投递消息
	bool PostThreadMessage(UINT uMessage, WPARAM wParam, LPARAM lParam);

	//事件函数
private:
	//开始事件
	virtual bool OnThreadStratEvent() { return true; }
	//停止事件
	virtual bool OnThreadStopEvent() { return true; }

	//内部函数
private:
	//运行函数
	virtual bool RepetitionRun()=NULL;
	//线程函数
	static unsigned __stdcall ThreadFunction(LPVOID pThreadData);
};

//////////////////////////////////////////////////////////////////////////

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -