FSBII(七)class CThread

转载 2012年03月26日 09:33:34
/********************************************************************
	created:	2003/02/14
	file base:	Thread
	file ext:	h
	author:		liupeng
	
	purpose:	
*********************************************************************/
#ifndef __INCLUDE_THREAD_H__
#define __INCLUDE_THREAD_H__

#if defined (_MSC_VER) && (_MSC_VER >= 1020)
	#pragma once
#endif

#ifndef _WINDOWS_
	#define WIN32_LEAN_AND_MEAN
		#include <windows.h>
	#undef WIN32_LEAN_AND_MEAN
#endif

/*
 * namespace OnlineGameLib::Win32
 */

namespace OnlineGameLib {
namespace Win32 {

/*
 * CThread
 */
class CThread 
{
public:
   
	CThread();
      
	virtual ~CThread();

	HANDLE GetHandle() const;

	void Wait() const;

	bool Wait(DWORD timeoutMillis) const;

	void Start();

	void Terminate( DWORD exitCode = 0 );

private:

	virtual int Run() = 0;

	static unsigned int __stdcall ThreadFunction( void *pV );

	HANDLE m_hThread;

	/*
	 * No copies do not implement
	 */
	CThread( const CThread &rhs );
	CThread &operator=( const CThread &rhs );

};

} // End of namespace OnlineGameLib
} // End of namespace Win32

#endif //__INCLUDE_THREAD_H__
#include "stdafx.h"
#include "KWin32.h"
#include "Thread.h"
#include "Macro.h"

#include <process.h>		//Thread define

#include "Win32Exception.h"

/*
 * namespace OnlineGameLib::Win32
 */

namespace OnlineGameLib {
namespace Win32 {

CThread::CThread()
	: m_hThread( NULL )
{

}
      
CThread::~CThread()
{
	SAFE_CLOSEHANDLE( m_hThread );
}

HANDLE CThread::GetHandle() const
{
	return m_hThread;
}

void CThread::Start()
{
	if ( m_hThread == NULL )
	{
		unsigned int threadID = 0;

		m_hThread = (HANDLE)::_beginthreadex(0, 
			0, 
			ThreadFunction,
			( void * )this, 
			0, 
			&threadID );

		if ( m_hThread == NULL )
		{
			throw CWin32Exception(_T("CThread::Start() - _beginthreadex"), GetLastError());
		}
	}
	else
	{
		throw CException(_T("CThread::Start()"), _T("Thread already running - you can only call Start() once!"));
	}
}

void CThread::Wait() const
{
	if ( !Wait( INFINITE ) )
	{
		throw CException(_T("CThread::Wait()"), _T("Unexpected timeout on infinite wait"));
	}
}

bool CThread::Wait(DWORD timeoutMillis) const
{
	bool ok;

	if ( !m_hThread )
	{
		return true;
	}

	DWORD result = ::WaitForSingleObject( m_hThread, timeoutMillis );

	if ( result == WAIT_TIMEOUT )
	{
		ok = false;
	}
	else if ( result == WAIT_OBJECT_0 )
	{
		ok = true;
	}
	else
	{
		throw CWin32Exception( _T( "CThread::Wait() - WaitForSingleObject" ), ::GetLastError() );
	}
    
	return ok;
}

unsigned int __stdcall CThread::ThreadFunction( void *pV )
{
	int result = 0;

	CThread* pThis = ( CThread * )pV;
   
	if ( pThis )
	{
		try
		{
			result = pThis->Run();
		}
		catch( ... )
		{
			TRACE( "CThread::ThreadFunction exception!" );
		}
	}

	return result;
}

void CThread::Terminate( DWORD exitCode /* = 0 */ )
{
	if ( m_hThread && !::TerminateThread( m_hThread, exitCode ) )
	{
		TRACE( "CThread::Terminate error!" );
	}

	SAFE_CLOSEHANDLE( m_hThread );
}

} // End of namespace OnlineGameLib
} // End of namespace Win32


相关文章推荐

FSBII(一)class CHyperlinkStatic : public CStatic

#if !defined(AFX_HYPERLINKSTATIC_H__32A71426_1315_407C_9D90_A484C5589D80__INCLUDED_) #define AFX_HYP...

FSBII(三)class CPictureEx : public CStatic

////////////////////////////////////////////////////////////////////// // PictureEx.cpp: implementat...

FSBII(六)IOCPServer

/******************************************************************** created: 2003/02/14 file bas...

FSBII(五)IOCP

/******************************************************************** created: 2003/02/14 file bas...

深入理解JVM(七)——Class文件结构

什么是JVM的“无关性”?Java具有平台无关性,也就是任何操作系统都能运行Java代码。之所以能实现这一点,是因为Java运行在虚拟机之上,不同的操作系统都拥有各自的Java虚拟机,因此Java能实...

Google's Python Class(七)——Python 实用工具

Python 实用工具

linux调度器(七)——other cfs class api and functions

OTHER CFS CLASS API set_curr_task_fair(rq):将cpu运行队列里的当前运行进程设置为cfs运行队列里当前运行的进程(set_curr_task_fair),对...

黑马程序员 JAVA增强<七> class类

------- android培训、java培训、期待与您交流! ---------- 一:class类:                    类的实例表示正在运行的 Java 应用程序...

Calling Java Class Methods from C with JNI

  • 2017年11月12日 14:06
  • 119KB
  • 下载

class文件反编译工具

  • 2017年10月27日 17:30
  • 690KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:FSBII(七)class CThread
举报原因:
原因补充:

(最多只允许输入30个字)