线程类封装

进程:一个操作系统中可以同时运行多个应用程序,每个应用程序被称为一个进程。即系统级别的多线程。

线程:一个程序同时可能有多个运行场景,每个运行场景就叫做一个线程。

Thread类的实例:代表一个并发任务,任何一个线程对象都是Thread类的实例,Thread类是线程的模版,它封装了

                复杂的线程开启等操作,也兼容不同的操作系统,并发任务的实现只要重写Thread的run方法即可。

 

具体实现:

1. 枚举线程的所有状态

enum ThreadStatus
{
    ThreadSuspended   = 0,    //挂起
    ThreadRunning     = 1,    //运行
    ThreadWaiting     = 2,    //等待
    ThreadTimeWaiting = 3,    //限时等待
    ThreadInitial     = 4,    //初始化
    ThreadCreated     = 5,    //创建
    ThreadFinished    = 6     //结束
};

 2. 枚举所有的线程优先级

enum ThreadPriority
{
    ThreadPriorityIdle          = 0,    // 空闲
    ThreadPriorityLowest        = 1,    // 最低
    ThreadPriorityLow           = 2,    // 低于正常
    ThreadPriorityNormal        = 3,    // 正常
    ThreadPriorityHigh          = 4,    // 高于正常
    ThreadPriorityHighest       = 5,    // 最高
    ThreadPriorityTimeCritical  = 6     // 实时
};

 3. therad类定义

class MThread
{
public:
    MThread();
    virtual ~MThread();

public:
    virtual void Start(ThreadPriority priority = ThreadPriorityNormal);
    virtual void Join();
    virtual void Cancel();
    virtual void Stop();
    void SetPriority(pthread_t threadID, ThreadPriority priority);
    void MsSleep(int milliseconds);
    pthread_t GetThreadId() const;
    ThreadPriority GetPriority() const;
    ThreadStatus GetThreadStatus() const;

private:
    static void* _ThreadFunc(void* arg);
    virtual void _Run() = 0;

private:
    pthread_t _thread;
    bool _threadAttatched;
    ThreadStatus _status;
};

 4. thread类实现

MThread::MThread()
    : _threadAttatched(false)
    , _status(ThreadInitial)
{
}

MThread::~MThread()
{
    Stop();
}

void MThread::Start(ThreadPriority priority)
{
    pthread_create(&_thread, NULL, _ThreadFunc, this);
    SetPriority(_thread, priority);
    _threadAttatched = true;
    _status = ThreadRunning;
}

void MThread::Join()
{
    if (_threadAttatched)
    {
        pthread_join(_thread, NULL);
        _threadAttatched = false;
    }
}

void MThread::Cancel()
{
    if (_threadAttatched)
    {
        pthread_cancel(_thread);
        _status = ThreadFinished;
    }
}

void MThread::Stop()
{
    if (_threadAttatched)
    {
        pthread_detach(_thread);   // 分离线程,子线程结束后自己回收资源
        _threadAttatched = false;
        _status = ThreadFinished;
    }
}

/*
 * 默认的调度策略 SCHED_OTHER 是不支持优先级使用的,
 * 而 SCHED_FIFO(先进先出) 和 SCHED_RR(时间片轮转) 支持优先级的使用,
 * 他们分别为1和99,数值越大优先级越高。
 */
void MThread::SetPriority(pthread_t threadID, Priority priority)
{
    int policy;
    struct sched_param param;
    pthread_getschedparam(threadID, &policy, &param);
    policy = SCHED_FIFO;
    param.sched_priority = ((priority - ThreadPriorityIdle) * (99 - 1) / ThreadPriorityTimeCritical) + 1;
    pthread_setschedparam(threadID, policy, &param);
}

void MThread::MsSleep(int milliseconds)
{
    usleep(milliseconds);
}

void* MThread::_ThreadFunc(void* arg)
{
    (reinterpret_cast<MThread*>(arg))->_Run();
    return NULL;
}

 

posted @ 2020-05-19 17:05  _yanghh  阅读(486)  评论(0)    收藏  举报