记录下自己进行资源mutex操作的API,方便以后使用。 #ifdef WIN32 typedef void wmutex; #else typedef pthread_mutex_t wmutex; #endif //创建mutex wmutex *create_mutex(char *name) { name = name; #ifdef WIN32 return((wmutex *)CreateMutex(NULL, FALSE, name)); #else wmutex *mutex; mutex = new wmutex; if(mutex == (wmutex *)NULL) { return((wmutex *)NULL); } if(pthread_mutex_init(mutex, NULL) != 0) { delete mutex; return(NULL); } else { return(mutex); } #endif } //对mutex进行lock void acquire_mutex(wmutex *pmutex) { #ifdef WIN32 WaitForSingleObject(pmutex, INFINITE); #else pthread_mutex_lock(pmutex); #endif } //对mutex进行unlock void release_mutex(wmutex *pmutex) { #ifdef WIN32 ReleaseMutex(pmutex); #else pthread_mutex_unlock(pmutex); #endif } //删除mutex变量 void destroy_mutex(wmutex *pmutex) { #ifdef WIN32 CloseHandle(pmutex); #else pthread_mutex_destroy(pmutex); delete pmutex; #endif }