Chinaunix首页 | 论坛 | 博客
  • 博客访问: 281080
  • 博文数量: 53
  • 博客积分: 1293
  • 博客等级: 中尉
  • 技术积分: 506
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-20 22:14
文章分类
文章存档

2014年(1)

2012年(5)

2011年(47)

分类: LINUX

2011-06-18 01:30:27

最近在用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);
    }
}
本篇文章来源于:开发学院    原文链接:
阅读(3574) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~