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

📄 win_thread.h

📁 积下的一点C++编程序库源码
💻 H
字号:
///////////////Win_Thread.h/////////////////////////
#ifndef WIN_THREAD_H
#define WIN_THREAD_H 1
//////////////////////////////////////////////////////////////////////////////


//文件名		: Win_Thread.h

//功能			: Win线程封装,应用于Windows系统

//创建/修改日期	: 2003.11.27

//作者			: 韩国静
//

#define UNI_THREADPROC 		DWORD WINAPI
//use for "UNI_THREADPROC ThreadProc (void *pThis);"

typedef DWORD (WINAPI *PUNI_THREADPROC)( void * pParam);


typedef HANDLE			UNI_ThreadHandle;
typedef UNI_DWORD			UNI_ThreadID;
typedef HANDLE	 		UNI_Semaphore;
#define UNI_Sleep(Milliseconds) (Sleep(Milliseconds))

////////////////////////////////	Windows线程互拆		////////
typedef CRITICAL_SECTION UNI_Mutex;

#define UNI_InitializeMutex(pMutex) (InitializeCriticalSection(pMutex),UNI_TRUE)

#define UNI_DestroyMutex(pMutex) (DeleteCriticalSection(pMutex),UNI_TRUE)

#define UNI_LockMutex(pMutex) (EnterCriticalSection(pMutex),UNI_TRUE)

#define UNI_UnLockMutex(pMutex) (LeaveCriticalSection(pMutex),UNI_TRUE)

///////////////////////
#define UNI_WAIT_FAILED		WAIT_FAILED
#define UNI_WAIT_TIMEOUT	WAIT_TIMEOUT

typedef HANDLE  UNI_Event;

#define UNI_InitEvent(pEvent) ((*(pEvent)=::CreateEvent(UNI_NULL,TRUE,FALSE,UNI_NULL))?UNI_TRUE:UNI_FALSE)

#define UNI_DestroyEvent(pEvent) (::CloseHandle(*(pEvent)),UNI_TRUE)

inline int UNI_WaitEvent(UNI_Event *pEvent,UNI_Mutex *,UNI_DWORD time)
{
	int ret=0;
	if(time!=0)
		ret=WaitForSingleObject(*pEvent,time);
	ResetEvent(*pEvent);
	return ret;
}

#define UNI_SetEvent(pEvent) (::SetEvent(*(pEvent))?UNI_TRUE:UNI_FALSE)

///////////////////////////////////////	Windows线程信号量	/////////////////////////////

#define  UNI_CreateSemaphore(pSemaphore,InitiValue)\
(*pSemaphore=CreateSemaphore(NULL,InitiValue,1000,NULL),(*pSemaphore!=NULL))

#define UNI_CloseSemaphore(Semaphore)\
(CloseHandle(Semaphore),UNI_TRUE)

#define UNI_WaitSemaphore(Semaphore)\
(WaitForSingleObject(Semaphore,INFINITE)!=WAIT_FAILED)

#define UNI_ReleaseSemaphore(Semaphore)\
ReleaseSemaphore(Semaphore,1,NULL)


////////////////////////////////	Windows线程局部存储		///////////////////////////////////
typedef UNI_DWORD UNI_TLSKEY;

#define UNI_TLSAlloc(pTlsKey) (*(pTlsKey)=::TlsAlloc(),\
(TLS_OUT_OF_INDEXES==*(pTlsKey))?UNI_FALSE:UNI_TRUE)

#define UNI_TLSFree(TlsKey) (::TlsFree(TlsKey)?UNI_TRUE:UNI_FALSE)

#define UNI_TlsSetValue(TlsKey,pVoid) (::TlsSetValue(TlsKey,(void *)pVoid)?UNI_TRUE:UNI_FALSE)

#define UNI_TlsGetValue(TlsKey) ::TlsGetValue(TlsKey)

//////////////////////////////////	Windows线程创建		////////////////////////////////////////////////
#define UNI_GetThreadId() GetCurrentThreadId()

#define UNI_ExitThread(ExitCode) return(DWORD) ExitCode

inline UNI_BOOL UNI_WaitCloseThread(UNI_ThreadHandle hThread)
{
	if(WaitForSingleObject(hThread,INFINITE)!=WAIT_FAILED)
	{
		::CloseHandle(hThread);
		return UNI_TRUE;
	}
	return UNI_FALSE;
}

inline UNI_BOOL UNI_CreateThread(PUNI_THREADPROC pThreadFunc,
								 void * param,
								 UNI_ThreadHandle *pHandle,UNI_ThreadID *pThreadID)
{
	*pHandle=(UNI_ThreadHandle )CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)pThreadFunc,(PVOID)param,0,pThreadID);
	if(*pHandle==UNI_NULL)
		return UNI_FALSE;
	return UNI_TRUE;
}

#define UNI_CloseThreadHandle(ThreadHandle)\
( CloseHandle(ThreadHandle))

inline UNI_BOOL UNI_SuspendThread(UNI_ThreadHandle hThread)
{
	return (-1==::SuspendThread(hThread))?UNI_FALSE:UNI_TRUE;
}

inline UNI_BOOL UNI_ResumeThread(UNI_ThreadHandle hThread)
{
	return (-1==::ResumeThread(hThread))?UNI_FALSE:UNI_TRUE;
}

inline UNI_BOOL UNI_KillThread(UNI_ThreadHandle hThread)
{
	return (::TerminateThread(hThread,0))?UNI_TRUE:UNI_FALSE;
}

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

#endif//WIN_THREAD_H

⌨️ 快捷键说明

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