Linux下C++类的线程函数

最近在用C++编写一个RTSP的客户端,由于要用到线程,根据C编程的经验,写了如下代码:

class LiRtspSession
{
    public:

       void* MainThreadFunc(void* pvData);

       void* AudioProcThreadFunc(void* pvData);
       void Connect();

       //省略的代码

    private:

        pthread_t     m_hProcHandle, m_hAudioProcHandle;

        //省略的代码

};

 

void LiRtspSession::Connect()
{
    //create main thread creating rtsp session and receiving rtp/rtcp packet
    int ret = pthread_create(&m_hProcHandle, NULL, MainThreadFunc, this);
    if(ret != 0)
    {
         return;
    }

 

    //create a thread receiving audio data
    if(m_bAudioEnabled && m_eProtoType == ptUDP);
    {
         ret = pthread_create(&m_hAudioProcHandle, NULL, AudioProcThreadFunc, this);
         if(ret != 0)
         {
             return;
         }
    }

    pthread_join(m_hProcHandle, NULL);
 
    if(m_bAudioEnabled && m_eProtoType == ptUDP);
    {
          pthread_join(m_hAudioProcHandle, NULL);
    }
}

 

但是在编译时却出现如下错误:

    LiRtspSession.cpp: In member function ‘void LiRtspSession::Connect()’:
    LiRtspSession.cpp:176: error: argument of type ‘void* (LiRtspSession::)(void*)’ does not match ‘void*(*) (void*)’

 

该错误表示:线程创建函数

     pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void*(*start_rtn) (void*),void *restrict arg) 

     的第三个参数类型为:void* (*)(void*),但我们在函数connect函数中给其传入的参数类型却是:

     void* (LiRtspSession::)(void*);于是就出现了类型不匹配的编译错误。

 

经过查看网上的相关讨论以及分析可知,要解决上述类型不匹配的问题有两个方法:

 

一、将线程函数定义为全局函数。

      这样线程函数的类型就为void* (*)(void*),于是传入的实参和函数形参就类型匹配了。而且为了使其能够访问类的成员,可以进一步

      将其定义为类的友元函数。

 

二、将线程函数定义为类的静态成员函数。

      由于c++程序在编译时,编译器会给类的成员函数强加一个this指针,所以成员函数的类型实际上为:

      void* (LiRtspSession::) (void*);

      我们可以通过将线程函数定义为静态函数来消除该this指针,这样线程函数的类型就变为void* (*)(void*)了。而且可以向线程函数

      传入一个this指针,这样线程函数就可以通过this指针来访问类的成员了。

 

考虑到C++编程的风格,我们在这里将线程函数定义为类的静态成员函数;于是,修改后的代码如下:

 

class LiRtspSession
{
    public:

       static void* MainThreadFunc(void* pvData);

       static void* AudioProcThreadFunc(void* pvData);
       void Connect();

       //省略的代码

    private:

        pthread_t     m_hProcHandle, m_hAudioProcHandle;

        //省略的代码

};

 

void* LiRtspSession::MainThreadFunc(void* pvData)

{

     LiRtspSession *pThiz = (LiRtspSession*)pvData;

     //省略的代码

     return NULL;

}

 

void LiRtspSession::Connect()
{
    //create main thread creating rtsp session and receiving rtp/rtcp packet
    int ret = pthread_create(&m_hProcHandle, NULL, MainThreadFunc, this);
    if(ret != 0)
    {
         return;
    }

 

    //create a thread receiving audio data
    if(m_bAudioEnabled && m_eProtoType == ptUDP);
    {
         ret = pthread_create(&m_hAudioProcHandle, NULL, AudioProcThreadFunc, this);
         if(ret != 0)
         {
             return;
         }
    }

    pthread_join(m_hProcHandle, NULL);
 
    if(m_bAudioEnabled && m_eProtoType == ptUDP);
    {
          pthread_join(m_hAudioProcHandle, NULL);
    }
}



 



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值