tars源码漫谈第27篇------tc_thread.h/tc_thread.cpp(包含线程锁的重要线程类)

      来看看tc_thread中的线程类, TC_ThreadControl是线程控制类, 对线程句柄进行操作,  常见操作如:

void TC_ThreadControl::join()
{
    if(pthread_self() == _thread)
    {
        throw TC_ThreadThreadControl_Exception("[TC_ThreadControl::join] can't be called in the same thread");
    }

    void* ignore = 0;
    int rc = pthread_join(_thread, &ignore);
    if(rc != 0)
    {
        throw TC_ThreadThreadControl_Exception("[TC_ThreadControl::join] pthread_join error ", rc);
    }
}

void TC_ThreadControl::detach()
{
    if(pthread_self() == _thread)
    {
        throw TC_ThreadThreadControl_Exception("[TC_ThreadControl::join] can't be called in the same thread");
    }

    int rc = pthread_detach(_thread);
    if(rc != 0)
    {
        throw TC_ThreadThreadControl_Exception("[TC_ThreadControl::join] pthread_join error", rc);
    }
}

       这很简单, 仅仅是封装而已。

 

       如下类是虚基类, easy:

class TC_Runable
{
public:
    virtual ~TC_Runable(){};
    virtual void run() = 0;
};

       在看看最重要的TC_Thread:

class TC_Thread : public TC_Runable
{
public:

    /**
     * @brief  构造函数
     */
    TC_Thread();

    /**
     * @brief  析构函数
     */
    virtual ~TC_Thread(){};

    /**
     * @brief  线程运行
     */
    TC_ThreadControl start();

    /**
     * @brief  获取线程控制类.
     *
     * @return ThreadControl
     */
    TC_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(TC_Thread *pThread);

    /**
     * @brief  运行
     */
    virtual void run() = 0;

protected:
    /**
     * 是否在运行
     */
    bool            _running;

    /**
     * 线程ID
     */
    pthread_t        _tid;

    /**
     * 线程锁
     */
    TC_ThreadLock   _lock;
};

}

      如下的_lock不就是我们的线程锁吗? 别忘了, 它兼具互斥锁和条件变量的管理能力

    /**
     * 线程锁
     */
    TC_ThreadLock   _lock;

      

      cpp中的的如下两个函数在tars框架中, 起到了重要的作用:

void TC_Thread::threadEntry(TC_Thread *pThread)
{
    pThread->_running = true;

    {
        TC_ThreadLock::Lock sync(pThread->_lock);
        pThread->_lock.notifyAll();
    }

    try
    {
        pThread->run();
    }
    catch(...)
    {
        pThread->_running = false;
        throw;
    }
    pThread->_running = false;
}

TC_ThreadControl TC_Thread::start()
{
    TC_ThreadLock::Lock sync(_lock);

    if(_running)
    {
        throw TC_ThreadThreadControl_Exception("[TC_Thread::start] thread has start");
    }

    int ret = pthread_create(&_tid,
                   0,
                   (void *(*)(void *))&threadEntry,
                   (void *)this);

    if(ret != 0)
    {
        throw TC_ThreadThreadControl_Exception("[TC_Thread::start] thread start error", ret);
    }

    _lock.wait();

    return TC_ThreadControl(_tid);
}

        其实, 意思也不复杂。 在后面介绍tars源码框架中, 我们会继续和上面两个函数打交道, 到时也会进一步叙述。

 

        总之, 搞懂TC_Thread类, 很关键。

        

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值