class CThread : public CObject
{
public:
CThread();
virtual ~CThread();
void start();
void join();
void detach();
pthread_t getThreadId() const
{
return m_threadId;
}
private:
virtual void run() = 0;
void createThread();
protected:
CThread(const CThread& other);
CThread& operator = (const CThread& other);
friend void * threadFunc (void * voidPointer);
pthread_t m_threadId;
};
CThread::CThread() :m_threadId (0)
{
;
}
CThread::~CThread()
{
;
}
void * threadFunc (void * voidPointer)
{
if (voidPointer == NULL)
{
throw CException ("threadFunc() : Paramater pass to thread is NULL");
}
CThread * thread = static_cast <CThread *> (voidPointer);
thread->run ();
return NULL;
}
void CThread::createThread ()
{
if (0 != pthread_create (&this->m_threadId, NULL, threadFunc, this))
{
throw CException("CThread::createThread fail");
}
}
void CThread::start ()
{
this->createThread ();
}
void CThread::join()
{
if (0 != pthread_join (this->m_threadId, NULL))
{
throw CException("CThread::join fail ");
}
}
void CThread::run()
{
;
}
void CThread::detach()
{
if (0 != pthread_detach(this->m_threadId))
{
throw CException("CThread::detach fail ");
}
}