标题:在QT程序中使用pthread的错误
作者:phenix_yw
日期:2006-05-30 22:13
内容:
用QT作一个接收GPS信号的程序,在GPSForm里开了两个线程,一个负责读串口的GPS接收的数据,另一个负责在界面中显示.用的是linux下的pthread库
在gpsform.cpp 中,
.....
void GPSForm::start()//由按钮触发这个事件
{
pthread_create(&rece_thread, NULL, receive, 0);
pthread_create(&show_thread, NULL, show_gps_info, 0);
}
void* GPSForm::receive(void *arg)
{
.....
}
void* GPSForm::show_gps_info(void *arg)
{
.......
}
可make时出现了这种错误:
gpsform.cpp:134: 错误:argument of type ‘void* (GPSForm::)(void*)’ does not match ‘void* (*)(void*)’
gpsform.cpp:135: 错误:argument of type ‘void* (GPSForm::)(void*)’ does not match ‘void* (*)(void*)’
单纯终端程序也是这样使用pthread的, 用g c c 编译通过,为什么用在q t 中用g++就有问题了呢?
请高手指点我该怎么改正.
不甚感激!
#1 [stylev 05-31 00:20]
void GPSForm::start() //由按钮触发这个事件
{
pthread_create(&rece_thread, NULL, ((void*)(*)(void*))receive, 0);
pthread_create(&show_thread, NULL,((void*)(*)(void*))show_gps_info, 0);
}
void GPSForm::receive(void *arg)
{
.....
}
void GPSForm::show_gps_info(void *arg)
{
.......
}
//这样试试
#2 [phenix_yw 06-01 18:24]
这样还是不行呀!强制转换不过来!
郁闷!!!
不过还是感谢stylev!
#3 [cocalele 06-01 22:21]
我猜你单纯的终端程序没有使用C++编写。GPSForm::show_gps_info(void *arg)
是类的成员函数,所以不能转换成void (*)(void *)类型。你可以试试把这两个函数定义成静态函数。
对于类成员函数, GPSForm::show_gps_info(void *arg),看上去是一个参数,实际上是两个参数GPSForm::show_gps_info(GPSForm* this, void *arg),所以不能强制转换。
当然,如果你使用了静态函数,就不能访问类的非静态成员变量了:)
#4 [phenix_yw 06-04 14:16]
问题正如cocalele所说,我的终端程序是用C写的。
现在我把void* receive(void *arg)和void* show_gps_info(void *arg)定义成全局函数,在类方法GPSForm::start()里,将this指针作为参数传递给receive和show_gps_info,
void GPSForm::start()
{
STOP=false;
GET_GPS_OK=false;
pthread_create(&rece_thread, NULL,receive, this);
pthread_create(&show_thread, NULL,show_gps_info, this);
}
现在终于可以运行成功了,不过总觉得这样有点别扭!呵呵
cocalele的方法我也会试一下,谢谢对一个新手的帮助!
#5 [jacklee 06-06 22:12]
一般开线程都这样
我在Windows下用MFC也是这样开的