为了实现windows下WaitForSingleObject等待互斥体的功能
我在Unix下写了一个
int WaitForSingleObject(pthread_mutex_t * mutex,int nMilliseconds)
{
if (nMilliseconds == INFINITE)
{
if (pthread_mutex_lock(mutex) != 0)
{
printf("pthread_mutex_lock error errno = %d\n");
return WAIT_FAILED;
}
return WAIT_OBJECT_0;
} else {
int nTime = nMilliseconds;
do {
int nRet = pthread_mutex_trylock(mutex);
switch (nRet)
{
case 0:
return WAIT_OBJECT_0;
case EBUSY:
{
Sleep(100);
nTime -= 100;
}
break;
default:
{
Sleep(100);
nTime -= 100;
}
break;
}
}while ( 0 < nTime );
return WAIT_TIMEOUT;
}
}
其中pthread_mutex_lock没有什么问题,pthread_mutex_trylock大多数时候也是正常的,但有时候pthread_mutex_trylock就返回22,为什么
红糖糍粑