線程控制類的封裝
線程常用方法
int pthread_join(pthread_t thread, void **retval);
int pthread_detach(pthread_t thread);
pthread_t pthread_self(void);
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
定義
/**
* @brief 線程控制異常類
*/
struct YR_ThreadThreadControl_Exception : public YR_Exception
{
YR_ThreadThreadControl_Exception(const string &buffer) : YR_Exception(buffer){};
YR_ThreadThreadControl_Exception(const string &buffer, int err) : YR_Exception(buffer, err){};
~YR_ThreadThreadControl_Exception() throw() {};
};
/**
* @brief 線程控制類
*/
class YR_ThreadControl
{
public:
/**
* @brief 構造, 表示當前運行的線程,
* join和detach在不能在該對象上調用
*/
YR_ThreadControl();
/**
* @return explicit
*/
explicit YR_ThreadControl(pthread_t);
/**
* @brief 等待當前線程結束, 不能在當前線程上調用
*/
void join();
/**
* @brief detach, 不能在當前線程上調用
*/
void detach();
/**
* @brief 獲取當前線程id.
*
* @return pthread_t當前線程id
*/
pthread_t id() const;
/**
* @brief 休息ms時間.
*
* @param millsecond 休息的時間,具體ms數字
*/
static void sleep(long millsecond);
/**
* @brief 交出當前線程控制權
*/
static void yield();
private:
pthread_t _thread;
};
實現
構造和析構
YR_ThreadControl::YR_ThreadControl(pthread_t thread) : _thread(thread)
{
}
YR_ThreadControl::YR_ThreadControl() : _thread(pthread_self())
{
}
控制方法
void YR_ThreadControl::join()
{
if(pthread_self() == _thread)
{
throw YR_ThreadThreadControl_Exception("[YR_ThreadControl::join] can't be called in the same thread");
}
void* ignore = 0;
int rc = pthread_join(_thread, &ignore);
if(rc != 0)
{
throw YR_ThreadThreadControl_Exception("[YR_ThreadControl::join] pthread_join error ", rc);
}
}
void YR_ThreadControl::detach()
{
if(pthread_self() == _thread)
{
throw YR_ThreadThreadControl_Exception("[YR_ThreadControl::join] can't be called in the same thread");
}
int rc = pthread_detach(_thread);
if(rc != 0)
{
throw YR_ThreadThreadControl_Exception("[YR_ThreadControl::join] pthread_join error", rc);
}
}
pthread_t YR_ThreadControl::id() const
{
return _thread;
}
void YR_ThreadControl::sleep(long millsecond)
{
struct timespec ts;
ts.tv_sec = millsecond / 1000;
ts.tv_nsec = (millsecond % 1000)*1000000;
nanosleep(&ts, 0);
}
void YR_ThreadControl::yield()
{
sched_yield();
}
線程基類的定義
class YR_Runable
{
public:
virtual ~YR_Runable(){};
virtual void run() = 0;
};
/**
* @brief 線程基類.
* 線程基類,所有自定義線程繼承於該類,同時實現run接口即可,
*
* 可以通過YR_ThreadContorl管理線程。
*/
class YR_Thread : public YR_Runable
{
public:
YR_Thread();
virtual ~YR_Thread(){};
/**
* @brief 線程運行
*/
YR_ThreadControl start();
/**
* @brief 獲取線程控制類.
*
* @return ThreadControl
*/
YR_ThreadControl getThreadControl() const;
/**
* @brief 線程是否存活.
*
* @return bool 存活返回true,否則返回false
*/
bool isAlive() const;
/**
* @brief 獲取線程id.
*
* @return pthread_t 線程id
*/
pthread_t id() { return _tid; }
protected:
/**
* @brief 靜態函數, 線程入口.
*
* @param pThread 線程對象
*/
static void threadEntry(YR_Thread *pThread);
/**
* @brief 運行
*/
virtual void run() = 0;
protected:
/**
* 是否在運行
*/
bool _running;
/**
* 線程ID
*/
pthread_t _tid;
/**
* 普通線程鎖
*/
YR_ThreadLock _lock;
};
實現
構造
YR_Thread::YR_Thread() : _running(false),_tid(-1)
{
}
線程入口
void YR_Thread::threadEntry(YR_Thread *pThread)
{
pThread->_running = true;
{
//YR_ThreadLock::Lock ->
// YR_Monitor<YR_ThreadMutex, YR_ThreadCond>::YR_LockT<YR_Monitor<YR_ThreadMutex,
// YR_ThreadCond>>
//構造時加鎖,析構時解鎖
YR_ThreadLock::Lock sync(pThread->_lock);
pThread->_lock.notifyAll();
}
try
{
pThread->run();
}
catch(...)
{
pThread->_running = false;
throw;
}
pThread->_running = false;
}
線程啓動
YR_ThreadControl YR_Thread::start()
{
//構造時加鎖,析構時解鎖
YR_ThreadLock::Lock sync(_lock);
if(_running)
{
throw YR_ThreadThreadControl_Exception("[YR_Thread::start] thread has start");
}
int ret = pthread_create(&_tid,
0,
(void *(*)(void *))&threadEntry,
(void *)this);
if(ret != 0)
{
throw YR_ThreadThreadControl_Exception("[YR_Thread::start] thread start error", ret);
}
_lock.wait();
return YR_ThreadControl(_tid);
}
其他函數實現
YR_ThreadControl YR_Thread::getThreadControl() const
{
return YR_ThreadControl(_tid);
}
bool YR_Thread::isAlive() const
{
return _running;
}