?? synchroobject.h
字號:
#ifndef __CSynchroObject_H__
#define __CSynchroObject_H__
//核心對象
#include "TypeDef.h"
#include "MyFuncDef.h"
#ifndef _WIN32
#include <semaphore.h>
#include <pthread.h>
#endif
//#pragma warning( disable : C4800 )
//***********************************************************//
//****************信號量(semaphore)API **********************//
//***********************************************************//
#ifdef _WIN32
#define TSEMAPHORE HANDLE
#else
#define TSEMAPHORE sem_t
#endif
#ifndef WIN32
#define INFINITE 0xFFFFFFFF //infinite timeout
#define WAIT_OBJECT_0 ((TDWORD)0x00000000L)
#define WAIT_TIMEOUT ((TDWORD)0x00000120L)
#endif
//創建信號量
inline int MyCreateSemaphore( TSEMAPHORE * pSem,TLONG lInitialCount,TLONG lMaximumCount,
TLPCSTR lpName )
{
int nRet = 0;
#ifdef _WIN32
*pSem = CreateSemaphore(NULL,lInitialCount,lMaximumCount,lpName);
if( *pSem==TNULL )
nRet = -1;
#else
nRet = sem_init(pSem,0,lInitialCount);
#endif
return nRet;
}
//釋放信號量
inline TBOOL MyReleaseSemaphore(
TSEMAPHORE * hSemaphore, //handle to semaphore
TLONG lReleaseCount, //count increament amount
TLPLONG lpPreviousCount) //previous count
{
#ifdef _WIN32
return ReleaseSemaphore(*hSemaphore,lReleaseCount,lpPreviousCount);
#else
for( int i=0; i<lReleaseCount; i++ )
{
int nRet = sem_post(hSemaphore);
if( nRet==-1 )
return TFALSE;
}
//lpPreviousCount 如果是Linux平臺下,則此參數無效
return TTRUE;
#endif
}
//等待信號量
inline TDWORD MyWaitForSingleSemaphore( TSEMAPHORE * pSemaphore,TDWORD dwMilliseconds )
{
#ifdef _WIN32
return WaitForSingleObject(*pSemaphore,dwMilliseconds);
#else
if( dwMilliseconds==0 )
{
int nRet=sem_trywait(pSemaphore);
if( nRet==0 )
return WAIT_OBJECT_0;
else
return WAIT_TIMEOUT;
}
else if( dwMilliseconds==INFINITE )
{
sem_wait(pSemaphore);
return WAIT_OBJECT_0;
}
//下面等待重試機制
if( dwMilliseconds>0 )
{
TDWORD dwTimer = dwMilliseconds;
while( dwTimer>0 )
{
int nRet = sem_trywait(pSemaphore);
if( nRet==0 )
return WAIT_OBJECT_0;
MySleep(1);
dwTimer--;
}
return WAIT_TIMEOUT;
}
#endif
}
inline void MyCloseSemaphore( TSEMAPHORE * pSemaphore )
{
#ifdef _WIN32
CloseHandle( *pSemaphore );
#else
sem_destroy( pSemaphore );
#endif
}
//***********************************************************//
//****************臨界區(CriticalSection)API ****************//
//***********************************************************//
#ifdef _WIN32
#define TCRITICAL_SECTION CRITICAL_SECTION
#else
#define TCRITICAL_SECTION pthread_mutex_t
#endif
inline void MyInitializeCriticalSection( TCRITICAL_SECTION* pCriticalSection )
{
#ifdef _WIN32
InitializeCriticalSection( pCriticalSection );
#else
pthread_mutex_init( pCriticalSection, TNULL );
#endif
}
inline void MyEnterCriticalSection( TCRITICAL_SECTION* pCriticalSection )
{
#ifdef _WIN32
EnterCriticalSection( pCriticalSection );
#else
pthread_mutex_lock( pCriticalSection );
#endif
}
inline void MyLeaveCriticalSection( TCRITICAL_SECTION* pCriticalSection )
{
#ifdef _WIN32
LeaveCriticalSection( pCriticalSection );
#else
pthread_mutex_unlock( pCriticalSection );
#endif
}
inline void MyDeleteCriticalSection( TCRITICAL_SECTION* pCriticalSection )
{
#ifdef _WIN32
DeleteCriticalSection( pCriticalSection );
#else
pthread_mutex_destroy( pCriticalSection );
#endif
}
//***********************************************************//
//****************事件[或條件變量]( Event )API ****************//
//***********************************************************//
#ifdef _WIN32
#define THEVENT HANDLE
#else
#define THEVENT pthread_cond_t
#endif
inline int MyCreateEvent( THEVENT * pHEvent )
{
#ifdef _WIN32
*pHEvent = CreateEvent( TNULL,TTRUE,TFALSE,TNULL );
if( *pHEvent== TNULL )
return -1;
return 0;
#else
pthread_cond_init( pHEvent, TNULL );
return 0;
#endif
}
inline int MySetEvent( THEVENT * pHEvent )
{
#ifdef _WIN32
int nRet = SetEvent( *pHEvent );
if( !nRet )
return -1;
return 0;
#else
#endif
}
inline int MyResetEvent( THEVENT * pHEvent )
{
#ifdef _WIN32
int nRet = ResetEvent( *pHEvent );
if( !nRet )
return -1;
return 0;
#else
#endif
}
//等待事件
inline TDWORD MyWaitForSingleEvent( THEVENT * pHEvent,TDWORD dwMilliseconds )
{
#ifdef _WIN32
return WaitForSingleObject( *pHEvent,dwMilliseconds );
#else
#endif
}
inline void MyCloseEvent( THEVENT * pHEvent )
{
#ifdef _WIN32
CloseHandle( *pHEvent );
#else
#endif
}
//******************************************************************************************//
//*********************************臨界區(CriticalSection)CLASS*****************************//
//******************************************************************************************//
class CriticalSection
{
public:
CriticalSection(void);
virtual ~CriticalSection(void);
void Enter();
void Leave();
private:
TCRITICAL_SECTION m_CritSect;
};
#endif //__CSynchroObject_H__
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -