error: expected expression before ‘;’ token的问题

来源:互联网 发布:珠江水质监测数据 编辑:程序博客网 时间:2024/05/29 11:41
在借用mosquitto源码,写一个订阅客户端时,使用了pthread_create和pthread_join时出现:
error: expected expression before ‘;’ token的问题:

相关代码为:

_ret = pthread_create(&g_clients_db.handle_thread_id, NULL, handle_thread,NULL);if(_ret != 0){printf("[create_handle_thread]:fail pthread_create:handle_thread()\n");return -40;}


_ret = pthread_join(g_clients_db.handle_thread_id,NULL); if(_ret != 0){printf("[join_handle_thread]:fail pthread_create:handle_thread()\n");return -4;}

奇怪之处还在于:去掉返回值后,即由:
_ret = pthread_create(&g_clients_db.handle_thread_id, NULL, handle_thread,NULL);

换成:
pthread_create(&g_clients_db.handle_thread_id, NULL, handle_thread,NULL);

就能编译通过。
但是实际执行时还是不能成功创建线程。
经过分析:
自己动手写一个线程程序,没有问题,可以继续执行,于是就怀疑引用的mosquitto的源码产生了该问题,在mosquitto的mosquitto_internal.h文件中有如下代码:
#if defined(WITH_THREADING) && !defined(WITH_BROKER)#  include <pthread.h>#else#  include "dummypthread.h"#endif

文件dummypthread.h中的内容为:

#ifndef _DUMMYPTHREAD_H_#define _DUMMYPTHREAD_H_#define pthread_create(A, B, C, D)#define pthread_join(A, B)#define pthread_cancel(A)#define pthread_mutex_init(A, B)#define pthread_mutex_destroy(A)#define pthread_mutex_lock(A) #define pthread_mutex_unlock(A) #endif

可见,是头文件dummypthread.h中对pthread_create、pthread_join之类的函数做了宏定义,在使用时调用的就不是系统的pthread_create、pthread_join函数而是这里定义的宏了,因此造成了上述问题。
问题清楚了,其解决办法也就有很多了,
例如可以在mosquitto_internal.h文件开始加上#define WITH_THREADING以去掉对文件dummypthread.h的引用。