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

📄 thread.h

📁 实时监控
💻 H
字号:
#ifndef _THREAD_H
#define _THREAD_H

#include "windows.h"

/// 线程封装.
class thread_base
{
private:
	HANDLE ht;	///< 线程句柄.
	volatile bool p;		///< 暂停标志.
	volatile bool x;		///< 退出标志.
	unsigned long id ;		//thread id
	
	/// 线程函数, 供CreateThread()使用.
	/// 该功能调用派生类的entry().
	static DWORD WINAPI _entry(LPVOID param)
	{
		thread_base* p = (thread_base*)param;
		p->entry() ;
		return 0;
	}

	/// 死等.
	void dead_wait()
	{
		// thread is not even started
		// then return immediately
		if( !ht )
			return;

		WaitForSingleObject(ht, INFINITE);
	}

	/// 关闭线程句柄.
	void close_handle()
	{
		if( !ht )
			return;

		CloseHandle(ht);
		ht = NULL;
	}
protected:
	
	/*! \brief 线程入口, 派生类必须实现!!!
	 *	在线程执行过程中, 派生类有责任在最经常执行的代码段中
	 *	时时检查thrd_check_pause()以及thrd_check_exit()
	 *	举例:
	 *	...
	 *	\code
	 *	if( thrd_check_exit() )
	 *		return;	// exit thread
	 *	thrd_check_pause();
	 *	...
	 *	\endcode
	 */
	virtual void entry() =0;

	
	/// 检查暂停请求
	inline void check_pause()
	{
		while(p)
		{
			Sleep(1);	// prevent from sucking all the system resources
		}
	}
	
	/// 检查退出请求.
	/// @return true 线程请求退出, false 忽略
	inline bool check_exit() const
	{
		return x;
	}

	/// 等待一会, 防止资源被独占
	void wait_a_while(){Sleep(1);}
public:
	/// 构造函数.
	thread_base():ht(NULL), x(false), p(false){}

	/// 析构函数.
	virtual ~thread_base(){thrd_close();}

	/// 判断线程是否结束.
	/// @return true 线程已经结束, false 线程还在运行.
	bool thrd_is_over()
	{
		// the thread is not even created
		if( !ht )
			return true;

		// check the thread handle
		return WaitForSingleObject(ht, 0) == WAIT_OBJECT_0;
	}

	/// @name 线程功能.
	//@{

	/// 启动线程.
	/// @return true 线程启动成功, false 线程启动失败
	virtual bool thrd_start(int nPriority)
	{
		// thread is still running
		if( !thrd_is_over() )
			return false;

		x = false;
		
		//				(SECU,STACK,ENTRY, PARAM,RUNNOW?, TH_ID)
		ht = CreateThread(NULL, 0, _entry, this,	0, &id) ;
		if (ht == NULL)  {
			char  sss[50] ;
			sprintf(sss, "CreateThread failed=%ld", GetLastError()) ;
			::MessageBox(NULL, sss, "error", MB_OK) ;
			return false;
		}
		SetThreadPriority(ht, nPriority) ;
		return true;
	}

	/// 暂停线程.
	virtual void thrd_pause()
	{
		p = true;
	}

	/// 继续暂停的线程.
	virtual void thrd_resume()
	{
		p = false;
	}

	/// 关闭线程.(停止线程)
	virtual DWORD thrd_close(DWORD timeout=5000)
	{
		// if thread is paused, then resume it.
		// so that it could have chance to check exit flag
		if( !ht )
			return 888 ;

		if( p )
			thrd_resume();
		x = true;
		//dead_wait();
		DWORD m_tembool = 9999 ;
	    m_tembool = WaitForSingleObject(ht, timeout) ;

		//close_handle();
		CloseHandle(ht);
		ht = NULL;

		return m_tembool ;
	}

	virtual DWORD thrd_terminate_close()
	{
		if( !ht )
			return 888 ;

		x = true;

		//AfxEndThread(0);
		CloseHandle(ht);
		ht = NULL;

		return 0 ;
	}

	bool thrd_is_paused() const {return p;}
	//@}

};

#define ID_SELF(x) TRACE(CString(x)+_T(" thrd id: %08lx\n"), GetCurrentThreadId());

#define THREAD_CHECK_EXIT if( check_exit() ) break;
#define THREAD_CHECK_PAUSE check_pause();
#define THREAD_CHECK THREAD_CHECK_EXIT; THREAD_CHECK_PAUSE;

#define THREAD_BEGIN for(;;) { THREAD_CHECK;
#define THREAD_END	 }

#endif	// _THREAD_H

⌨️ 快捷键说明

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