comparing thread API between Linux and Windows

表 1. 线程函数列表

对象操作Linux Pthread APIWindows SDK 库对应 API
线程创建pthread_createCreateThread
退出pthread_exitThreadExit
等待pthread_joinWaitForSingleObject
互斥锁创建pthread_mutex_initCreateMutex
销毁pthread_mutex_destroyCloseHandle
加锁pthread_mutex_lockWaitForSingleObject
解锁pthread_mutex_unlockReleaseMutex
条件创建pthread_cond_initCreateEvent
销毁pthread_cond_destroyCloseHandle
触发pthread_cond_signalSetEvent
广播pthread_cond_broadcastSetEvent / ResetEvent
等待pthread_cond_wait / pthread_cond_timedwaitSingleObjectAndWait

for more:http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html

 封装部分API

// mutex
#ifdef WIN32
 #include <windows.h>
 #include <process.h>
#else
 #include <pthread.h>
#endif

class CMutex
{
public:
#ifdef WIN32
    void Initialize( ) { InitializeCriticalSection( &cs ); }
    void Destroy( ) { DeleteCriticalSection( &cs ); }
    void Claim( ) { EnterCriticalSection( &cs ); }
    void Release( ) { LeaveCriticalSection( &cs ); }

    CRITICAL_SECTION cs;
#else
    void Initialize( ) { pthread_mutex_init( &mtx, NULL ); }
    void Destroy( ) { pthread_mutex_destroy( &mtx ); }
    void Claim( ) { pthread_mutex_lock( &mtx ); }
    void Release( ) { pthread_mutex_unlock( &mtx ); }

    pthread_mutex_t mtx;
#endif
};
原文地址:https://www.cnblogs.com/no7dw/p/2339398.html