Linux下C++类的线程函数
来源:互联网 发布:赌钱麻将软件 编辑:程序博客网 时间:2024/06/03 14:37
最近在用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);
}
}
- Linux下C++类的线程函数
- Linux下C++类的线程函数
- linux 下的C函数
- linux下c线程
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- linux下C语言线程的介绍
- Linux下C线程池的实现
- Linux下的C语言线程池
- linux下c的线程池
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux C下的线程操作函数笔记及实例代码
- {linux C}之linux下的C线程池
- Linux下非线程安全的函数及替代函数
- Linux下线程操作函数
- 有关EXTJS desktop的文章
- 微软Silverlight
- Ext-API详解
- try,except异常处理
- 网络层访问权限控制技术-ACL详解 (2)
- Linux下C++类的线程函数
- 网络层访问权限控制技术-ACL详解 (3)
- ld.so.conf 文件与PKG_CONFIG_PATH变量
- MyEclipse 管理配置JDK
- Installshield不显示准备安装界面
- 修改Win7的防火墙配置实现IIS 7远程访问
- 谈谈显存的使用分析(一)
- SWT窗口预览
- 张亚勤新力作《变革中的思索》受热捧