為了實(shí)現(xiàn)windows下WaitForSingleObject等待互斥體的功能我在Unix下寫了一個(gè)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大多數(shù)時(shí)候也是正常的,但有時(shí)候pthread_mutex_trylock就返回22,為什么
而且只要返回一次22整個(gè)循環(huán)中就都是22一直到循環(huán)結(jié)束,高手幫忙看看這是什么錯誤?
慕桂英3389331
2023-03-03 14:10:18