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

📄 thread.h

📁 文件加密解密源代码
💻 H
字号:
#ifndef THREAD_H_INCLUDED
#define THREAD_H_INCLUDED

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///[Thread - Parent class for classes who want to be executed in new thread]////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// This class is for performing functionality without hanging the calling thread.
/// Usage: implementing run() method and call whenever required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Written by Nir Dremer
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace GenLib
{

class Thread
{
public:
	Thread();
    ~Thread();

	/*
		Start thread
		Suspended - set true to start in suspended mode
		Returns handle of thread if successful, otherwise NULL
	*/
	HANDLE start(const char *name, const bool suspended = false);

	/*
		Switch to "sleep" mode
		timeMS    - time in ms to sleep
		alertable - set true to wake up on alert
	*/
	void sleep(const unsigned long timeMS);

	/*
		Terminate thread
		to wait for termination, use waitForExit().
	*/
	void terminate() {  _terminationRequested = true; }

	/*
		Terminate and wait for termination
	*/
	void terminateAndWait()	{  terminate();   waitForExit(); }

	/*
		Wait for termination
	*/
    void waitForExit();

	/*
		Returns true if thread is terminating
	*/
	bool shouldTerminate() const { return _terminationRequested; }

	/*
		Exit code of terminated thread
	*/
	unsigned long getExitCode() const;

	/*
		Handle of thread
	*/
	HANDLE getHandle() { return _handle; }

	/*
		ID of thread
	*/
	DWORD getID() { return _ID; }

	/*
		Suspend/Resume thread
	*/
	unsigned long suspend();
	unsigned long resume();

	/*
		Thread priority
	*/
	bool	setPriority	(int priority);
	int		getPriority	() const;

	bool	sleep(unsigned long timeMS, bool alertable );

protected:
	/*
		Finish thread
		Call to start again only and after terminateAndWait()
	*/
	void finish();


	/*
		Main thread loop
		Base class do nothing, returns immediately
	*/
	virtual int run();

	/*
		Will enter when terminating
		Base class do nothing
	*/
	virtual void shutDown();

	void exit(unsigned long code);

	static unsigned __stdcall execute(void* thread);

private:
	unsigned		_ID;
	HANDLE          _handle;
	bool            _terminationRequested;
};

};

#endif // #ifdef THREAD_H_INCLUDED

⌨️ 快捷键说明

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