新增:实现 ThreadMutex 类

This commit is contained in:
2025-09-01 16:35:25 +08:00
commit 9958d730d9
8 changed files with 259 additions and 0 deletions

53
code/ThreadMutex.cpp Normal file
View 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_);
}