?? win_thread.h
字號:
///////////////Win_Thread.h/////////////////////////
#ifndef WIN_THREAD_H
#define WIN_THREAD_H 1
//////////////////////////////////////////////////////////////////////////////
//文件名 : Win_Thread.h
//功能 : Win線程封裝,應(yīng)用于Windows系統(tǒng)
//創(chuàng)建/修改日期 : 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線程創(chuàng)建 ////////////////////////////////////////////////
#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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -