📄 thread.cpp
字号:
#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include "thread.h"pthread_key_t g_keyself;void* thread_start( void* pvoid ){ CThread* pth; pthread_setspecific( g_keyself, pvoid ); pth = (CThread*)pvoid; if( pth->Init() ) { pth->Run(); } pth->m_retval = pth->Exit(); return NULL;}CMutex::CMutex( void ){ pthread_mutex_init( &m_mutex, NULL );}CMutex::~CMutex( void ){ pthread_mutex_destroy( &m_mutex );}void CMutex::Lock( void ){ pthread_mutex_lock( &m_mutex );}void CMutex::Unlock( void ){ pthread_mutex_unlock( &m_mutex );}CSemaphore::CSemaphore( UINT nCount ){ pthread_mutex_init( &m_mutex, NULL ); pthread_cond_init( &m_cond, NULL ); m_count = nCount;}CSemaphore::~CSemaphore( void ){ pthread_cond_destroy( &m_cond ); pthread_mutex_destroy( &m_mutex );}void CSemaphore::Lock( void ){ pthread_mutex_lock( &m_mutex ); while( m_count == 0 ) { pthread_cond_wait( &m_cond, &m_mutex ); } m_count--; pthread_mutex_unlock( &m_mutex );}void CSemaphore::Unlock( void ){ pthread_mutex_lock( &m_mutex ); m_count++; pthread_mutex_unlock( &m_mutex ); pthread_cond_signal( &m_cond );}/**************************************************************************** * * CThread * ****************************************************************************/CThread::CThread( void ){ // Empty}CThread::~CThread( void ){ // Empty}void CThread::Create( void ){ pthread_create( &m_thread, NULL, thread_start, this );}bool CThread::Init( void ){ return false;}int CThread::Exit( void ){ return 0;}void CThread::Run( void ){}CThread* CThread::This( void ){ return (CThread*)pthread_getspecific( g_keyself );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -