thread.cpp

来自「dget是一个基于Linux平台的多线程下载工具, 采用C++开发。主要支持FT」· C++ 代码 · 共 116 行

CPP
116
字号
#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 + =
减小字号Ctrl + -
显示快捷键?