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

📄 gathreading.h

📁 遗传算法做的排课系统
💻 H
字号:

#ifndef __GA_THREADING_H__
#define __GA_THREADING_H__

#include "..\ExportImport.h"
#include "..\CallConvention.h"

#ifdef WIN32

// 32-bits Windows headers
#include <windows.h>

#else

#ifdef LINUX
// Linux headers
#endif // LINUX

#endif // WIN32

namespace Threading
{

	#ifdef WIN32
	
	// Operating system specific thread structure
	typedef HANDLE SystemThread;

	// Operating system specific synchronization object
	typedef CRITICAL_SECTION SysSyncObject;

	// Operating system specific semaphore object
	typedef HANDLE SysSemaphoreObject;

	// Thread function's return type
	typedef int ThreadFunctionReturn;

	// Type of thread ID
	typedef DWORD ThreadID;

	// make and free semaphore
	#define MAKE_SEMAPHORE(SEMAPHORE_NAME, MAX_COUNT, INITIAL_COUNT) \
		SEMAPHORE_NAME = CreateSemaphore( NULL, INITIAL_COUNT, MAX_COUNT, NULL )
	#define FREE_SEMAPHORE(SEMAPHORE_NAME) CloseHandle( SEMAPHORE_NAME )

	// locking and unlocking synchronization objects
	#define LOCK(LOCK_NAME) ( LOCK_NAME ).Lock()
	#define UNLOCK(LOCK_NAME) ( LOCK_NAME ).Unlock()
	#define LOCK_SEMAPHORE(SEMAPHORE_NAME, COUNT) \
		for( int lock_cnt##__LINE__ = 0; lock_cnt##__LINE__ < ( COUNT ); lock_cnt##__LINE__++ ) \
			WaitForSingleObject( SEMAPHORE_NAME, INFINITE )
	#define UNLOCK_SEMAPHORE(SEMAPHORE_NAME, COUNT) ReleaseSemaphore( SEMAPHORE_NAME, COUNT, NULL )

	// defines class with synchronizator
	#define DEFINE_SYNC_CLASS	protected: mutable Threading::GaCriticalSection _synchronizator; \
								public: Threading::GaCriticalSection* GACALL GetSynchronizator() const \
											{ return &_synchronizator; }

	// locks object with synchronizator
	#define LOCK_OBJECT(LOCK_NAME, OBJECT) GaSectionLock LOCK_NAME( ( OBJECT )->GetSynchronizator(), true )
	#define LOCK_THIS_OBJECT(LOCK_NAME) GaSectionLock LOCK_NAME( &this->_synchronizator, true )

	// atomic operations
	#define ATOMIC_EXCHANGE_ADD(OLD, DESTINATION, VALUE) OLD = InterlockedExchangeAdd( (LONG volatile*) &( DESTINATION ), ( VALUE ) )
	#define ATOMIC_INC(VALUE) InterlockedIncrement( (LONG volatile*) &VALUE )
	#define ATOMIC_DEC(VALUE) InterlockedDecrement( (LONG volatile*) &VALUE )
	#define ATOMIC_EXCHANGE(DESTINATION, VALUE) InterlockedExchange( (LONG volatile*) &DESTINATION, VALUE )

	#else

	#ifdef LINUX
	#endif // LINUX

	#endif // WIN32

	class GaThread;

	// Prototype of thread function
	typedef ThreadFunctionReturn (GACALL *ThreadFunctionPointer)(GaThread*, void*);

	// Parameters of a new thread
	struct GaThreadParameter
	{

	public:

		// Pointer to function which will be executed in new thread
		ThreadFunctionPointer _functionPointer;

		// Additinal parameter for the function
		void* _functionParameters;

	};// END STRUCTURE DEFINITION GaThreadParameter

	// Status of a thread
	enum GaThreadStatus
	{
		GATS_RUNNING = 0x1, 
		GATS_STOPPED = 0x2, 
		GATS_PAUSED = 0x4, 
		GATS_NOT_RUNNING = GATS_STOPPED | GATS_PAUSED
	};

	// #pragma region GaCriticalSection

	// Synchronization object for critical sections
	class GaCriticalSection
	{

	private:

		// Critical section data
		SysSyncObject _section;

	public:

		// Initialization of critical section
		DLL_EXPORT
		GaCriticalSection();

		// Free aquired resources
		DLL_EXPORT
		~GaCriticalSection();

		// Locks acess to the section
		DLL_EXPORT
		void GACALL Lock();

		// Unlocks acess to the section
		DLL_EXPORT
		void GACALL Unlock();

	};// END CLASS DEFINITION GaCriticalSection

	// #pragma endregion

	// #pragma region GaSectionLock

	// Automatic handling for critical sections
	class GaSectionLock
	{

	private:

		// Section controlled by lock
		GaCriticalSection* _section;

		// Indicate that critical section is locked
		bool _locked;

	public:

		// Initialization of lock
		DLL_EXPORT
		GaSectionLock(GaCriticalSection* section,
			bool aquireLock);

		// Auto unlocking of critical section
		DLL_EXPORT
		~GaSectionLock();

		// Locks the section
		DLL_EXPORT
		void GACALL Lock();

		// Unlocks the section
		DLL_EXPORT
		void GACALL Unlock();

	};// END CLASS DEFINITION GaSectionLock

	// #pragma endregion

	// #pragma region GaThread

	// Controls of threads
	class GaThread
	{
		DEFINE_SYNC_CLASS

	private:

		// Status of the thread
		GaThreadStatus _status;

		// Operating system specific thread information
		SystemThread _thread;

		// Id of the thread
		ThreadID _id;

		// Parameters of the thread
		GaThreadParameter _parameters;

	public:

		// Initialize thread parameters
		DLL_EXPORT
		GaThread(const GaThreadParameter& parameters,
		 bool started);

		// Frees aquired resources.
		DLL_EXPORT
		~GaThread();

		// Starts thread if it is not running.
		// Returns TRUE if thread is started.
		DLL_EXPORT
		bool GACALL Start();

		// Temproary pause the execution of the thread and saves current state.
		// Returns TRUE if execution is paused.
		DLL_EXPORT
		bool GACALL Pause();

		// Stop the execution of the thread and discarge current state.
		// Returns TRUE if execution is stopped.
		DLL_EXPORT
		bool GACALL Abort();

		// Wait for thread to finish
		DLL_EXPORT
		bool GACALL Join();

		// Returns the thread status,
		DLL_EXPORT
		GaThreadStatus GACALL Status();

		// Returns ID of the thread
		DLL_EXPORT
		ThreadID GACALL GetId();

	private:

		// Wraper for thread function
		static ThreadFunctionReturn APICALL ThreadFunctionWraper(GaThread* thread);

	};// END CLASS DEFINITION GaThread

	// #pragma endregion

} // Threading

#endif // __GA_THREADING_H__

⌨️ 快捷键说明

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