新增:实现 ThreadMutex 类
This commit is contained in:
53
code/ThreadMutex.cpp
Normal file
53
code/ThreadMutex.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "ThreadMutex.h"
|
||||
|
||||
int ThreadMutex::XLockGetTestFlag(void)
|
||||
{
|
||||
return mCurTestFlag_;
|
||||
}
|
||||
|
||||
bool ThreadMutex::XLockTryLock(int tInputWait10Ms, int tInputTestFlag)
|
||||
{
|
||||
int tTempN;
|
||||
|
||||
if (tInputWait10Ms <= 0)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if (0 == pthread_mutex_trylock(&mMutex_))
|
||||
{
|
||||
mCurTestFlag_ = tInputTestFlag;
|
||||
return true;
|
||||
}
|
||||
usleep(1000 * 5);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (tTempN = 0; tTempN < tInputWait10Ms; tTempN++)
|
||||
{
|
||||
if (0 == pthread_mutex_trylock(&mMutex_))
|
||||
{
|
||||
mCurTestFlag_ = tInputTestFlag;
|
||||
return true;
|
||||
}
|
||||
usleep(1000 * 5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadMutex::XLockUnLock(void)
|
||||
{
|
||||
mCurTestFlag_ = -1;
|
||||
pthread_mutex_unlock(&mMutex_);
|
||||
}
|
||||
|
||||
ThreadMutex::ThreadMutex(void)
|
||||
{
|
||||
pthread_mutex_init(&mMutex_, NULL);
|
||||
}
|
||||
|
||||
ThreadMutex::~ThreadMutex()
|
||||
{
|
||||
pthread_mutex_destroy(&mMutex_);
|
||||
}
|
||||
Reference in New Issue
Block a user