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

📄 linux_thread.h

📁 积下的一点C++编程序库源码
💻 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 + -