表 1. 线程函数列表

对象 操作 Linux Pthread API Windows SDK 库对应 API
线程 创建 pthread_create CreateThread
退出 pthread_exit ThreadExit
等待 pthread_join WaitForSingleObject
互斥锁 创建 pthread_mutex_init CreateMutex
销毁 pthread_mutex_destroy CloseHandle
加锁 pthread_mutex_lock WaitForSingleObject
解锁 pthread_mutex_unlock ReleaseMutex
条件 创建 pthread_cond_init CreateEvent
销毁 pthread_cond_destroy CloseHandle
触发 pthread_cond_signal SetEvent
广播 pthread_cond_broadcast SetEvent / ResetEvent
等待 pthread_cond_wait / pthread_cond_timedwait SingleObjectAndWait

 

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
};

 

 

相关文章:

  • 2021-06-09
  • 2021-03-31
  • 2022-02-20
  • 2021-08-26
  • 2022-12-23
  • 2021-11-08
  • 2022-02-15
猜你喜欢
  • 2021-05-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2021-04-17
  • 2021-05-09
  • 2021-07-20
相关资源
相似解决方案