《ASCE1885的源码分析》の跨平台线程对象Thread封装基类

时间:2021-02-24 20:02:01

首先当然是先定义头文件Thread.h咯,如下:

#ifndef _THREAD_H

#define _THREAD_H

 

#ifdef _WIN32

#include <windows.h>

#else

#include <pthread.h>

#endif

 

#ifdef _WIN32

typedef unsigned threadfunc_t;

typedef void* threadparam_t;

#define STDPREFIX __stdcall

#else

typedef void* threadfunc_t;

typedef void* threadparam_t;

#define STDPREFIX

#endif

 

//线程的基类

class Thread

{

public:

    Thread(bool release=true);

    virtual ~Thread();

   

    static threadfunc_t STDPREFIX StartThread(threadparam_t);

   

    virtual void Run()=0;  //纯虚函数,由派生类实现

   

#ifdef _WIN32

    HANDLE GetThread(){return m_thread;}

    unsigned GetThreadId(){return m_dwThreadId;}

#else

    pthread_t GetThread(){return m_thread;}

#endif

 

    bool IsRunning();

    void SetRunning(bool x);

    bool IsReleased();

    void SetRelease(bool x);

    bool DeleteOnExit();

    void SetDeleteOnExit(bool x=true);

    bool IsDestructor();

   

protected:

#ifdef _WIN32

    HANDLE m_thread;

    unsigned m_dwThreadId;

#else

    pthread_t m_thread;

#endif

 

private:

    Thread(const Thread&){}

    Thread& operator=(const Thread&){return *this;}

    bool m_running;

    bool m_release;

    bool m_b_delete_on_exit;

    bool m_b_destructor;

};

 

#endif

 

接着是实现文件Thread.cpp,由于代码比较简单,就不多解释了,直接看代码里的注释就行了:

#include <stdio.h>

#ifdef _WIN32

#include <process.h>

#else

#include <unistd.h>

#endif

 

#include "Thread.h"

 

Thread::Thread(bool release)

    :m_thread(0),

    m_running(true),

    m_release(false),

    m_b_delete_on_exit(false),

    m_b_destructor(false)

{

#ifdef _WIN32

    m_thread = (HANDLE)_beginthreadex(NULL, 0, &StartThread,

                                            this, 0, &m_dwThreadId);   

#else

    pthread_attr_t attr;

    pthread_attr_init(&attr);

    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    if(pthread_create(&m_thread, &attr, StartThread, this) == 1)

    {

        perror("Thread:create failed");

        SetRunning(false);

    }

#endif

    m_release = release;

}           

 

Thread::~Thread()

{

    m_b_destructor = true;

    if(m_running)

    {

        SetRelease(true);

        SetRunning(false);

#ifdef _WIN32

        Sleep(1000);

#else

        sleep(1);

#endif           

    }

#ifdef _WIN32

    if(m_thread)

        ::CloseHandle(m_thread);

#endif                  

}

 

//线程函数

threadfunc_t STDPREFIX Thread::StartThread(threadparam_t zz)

{

    Thread *p = (Thread*)zz;

    while(p->m_running && !p->m_release)

    {

#ifdef _WIN32

        Sleep(1000);

#else

        sleep(1);

#endif                  

    }

   

    if(p->m_running)

    {

        p->Run();              

    }

    p->SetRunning(false); //Run()运行完了,或根本没运行

    if(p->DeleteOnExit() && !p->IsDestructor())

    {

        delete p;                    

    }

#ifdef _WIN32

    _endthreadex(0);

#endif

    return (threadfunc_t)NULL;

}

 

bool Thread::IsRunning()

{

    return m_running;    

}

 

void Thread::SetRunning(bool x)

{

    m_running = x;    

}

 

bool Thread::IsReleased()

{

    return m_release;    

}

 

void Thread::SetRelease(bool x)

{

    m_release = x;    

}

 

//退出时是否释放线程对象Thread*

bool Thread::DeleteOnExit()

{

    return m_b_delete_on_exit;    

}

 

void Thread::SetDeleteOnExit(bool x)

{

    m_b_delete_on_exit = x;    

}

 

//是否已经调用了析构函数

bool Thread::IsDestructor()

{

   return m_b_destructor;    

}