?? linux_thread.h
字號:
////////////Linux_Thread.h/////////////////////////////////
#ifndef LINUX_THREAD_H
#define LINUX_THREAD_H 1
//////////////////////////////////////////////////////////////////////////////
//文件名 : Linux_Thread.h
//功能 :pthread庫封裝,應用于Linux系統
//創建/修改日期 : 2003.11.27
//作者 : 韓國靜
//
/////////////////////////////////////Linux////////////////////////////////////////////////////////
#include "pthread.h"
#include "semaphore.h"
#include "unistd.h"
#include <sys/time.h>
#define UNI_THREADPROC void* typedef void* (*PUNI_THREADPROC)( void * pParam);
typedef sem_t UNI_Semaphore;
typedef pthread_t UNI_ThreadHandle;
typedef pthread_t UNI_ThreadID;
#define UNI_Sleep(Milliseconds) do{sleep(Milliseconds/1000);}while(0)
//////////////////////////////////Linux線程信號量/////////////////////////////////////////////////
#define UNI_CreateSemaphore(pSemaphore,InitiValue)\
(0==sem_init(pSemaphore,0,InitiValue))
#define UNI_CloseSemaphore(Semaphore)\
(0==sem_destroy(&Semaphore))
#define UNI_WaitSemaphore(Semaphore)\
(sem_wait(&Semaphore)==0)
#define UNI_ReleaseSemaphore(Semaphore)\
(0==sem_post(&Semaphore))
/////////////////////////////////////Linux線程互拆///////////////////////////////////
typedef pthread_mutex_t UNI_Mutex;
#define UNI_InitializeMutex(pMutex) (pthread_mutex_init(pMutex,UNI_NULL)?UNI_FALSE:UNI_TRUE)
#define UNI_DestroyMutex(pMutex) (pthread_mutex_destroy(pMutex)?UNI_FALSE:UNI_TRUE)
#define UNI_LockMutex(pMutex) (pthread_mutex_lock(pMutex)?UNI_FALSE:UNI_TRUE)
#define UNI_UnLockMutex(pMutex) (pthread_mutex_unlock(pMutex)?UNI_FALSE:UNI_TRUE)
//////////////////////////////////Linux條件等待/////////////////////////////////////////
#define UNI_WAIT_FAILED -1//WAIT_FAILED
#define UNI_WAIT_TIMEOUT -1//ETIMEDOUT
typedef pthread_cond_t UNI_Event;
#define UNI_InitEvent(pEvent) ((0==pthread_cond_init(pEvent,UNI_NULL))?UNI_TRUE:UNI_FALSE)
#define UNI_DestroyEvent(pEvent) ((0==pthread_cond_destroy(pEvent))?UNI_TRUE:UNI_FALSE)
/*
inline int UNI_WaitEvent(UNI_Event *pEvent,UNI_Mutex *pMutex,UNI_DWORD time)
{
struct timeval now;
struct timespec timeout;
pthread_mutex_lock(pMutex);
gettimeofday(&now,UNI_NULL);
timeout.tv_sec = now.tv_sec +(time/1000);
timeout.tv_nsec = now.tv_usec * 1000+(time%1000);
int retcode = pthread_cond_timedwait(pEvent,pMutex, &timeout);
pthread_mutex_unlock(pMutex);
return retcode?-1:0;
}
*/
inline int UNI_WaitEvent(UNI_Event *pEvent,UNI_Mutex *pMutex,UNI_DWORD time)
{
struct timeval now;
struct timespec timeout;
gettimeofday(&now,UNI_NULL);
timeout.tv_sec = now.tv_sec +(time/1000);
timeout.tv_nsec = now.tv_usec * 1000+(time%1000);
int retcode = pthread_cond_timedwait(pEvent,pMutex, &timeout);
return retcode?-1:0;
}
#define UNI_SetEvent(pEvent) (pthread_cond_signal(pEvent))
///////////////////////////////Linux線程局部存儲/////////////////////////////////////////////////
typedef pthread_key_t UNI_TLSKEY;
#define UNI_TLSAlloc(pTlsKey) (pthread_key_create(pTlsKey,UNI_NULL)==0)?UNI_TRUE:UNI_FALSE
#define UNI_TLSFree(TlsKey) (pthread_key_delete(TlsKey)==0)?UNI_TRUE:UNI_FALSE
#define UNI_TlsSetValue(TlsKey,pVoid) (pthread_setspecific(TlsKey,(void *)pVoid)==0)?UNI_TRUE:UNI_FALSE
#define UNI_TlsGetValue(TlsKey) pthread_getspecific(TlsKey)
////////////////////////////////////Linux線程創建/////////////////////////////////////////////
#define UNI_GetThreadId() pthread_self()
#define UNI_ExitThread(ExitCode) return (void *)ExitCode
inline UNI_BOOL UNI_WaitCloseThread(UNI_ThreadHandle hThread)
{
pthread_join(hThread,(void **)0);
return UNI_TRUE;
}
inline UNI_BOOL UNI_ThreadEqual(UNI_ThreadID tid1,UNI_ThreadID tid2)
{
return pthread_equal(tid1,tid2)?UNI_TRUE:UNI_FALSE;
}
inline UNI_BOOL UNI_CreateThread(UNI_THREADPROC (*pThreadFunc)(void *),
void *Param,
UNI_ThreadHandle *pHandle,
UNI_ThreadID *pThreadID)
{
if(0!=pthread_create(pHandle,
(pthread_attr_t *)UNI_NULL,
pThreadFunc,
Param))
return UNI_FALSE;
if(pThreadID)
*pThreadID=(UNI_ThreadID)(*pHandle);
return UNI_TRUE;
}
#define UNI_CloseThreadHandle(ThreadHandle) (pthread_detach(ThreadHandle)==0)inline UNI_BOOL UNI_SuspendThread(UNI_ThreadHandle hThread)
{
return UNI_FALSE;
}
inline UNI_BOOL UNI_ResumeThread(UNI_ThreadHandle hThread)
{
return UNI_FALSE;
}
inline UNI_BOOL UNI_KillThread(UNI_ThreadHandle hThread)
{
return UNI_FALSE;
}
////////////////////////////////////////////////////////////
#endif//LINUX_THREAD_H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -