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

117 篇文章 6 订阅
在借用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的引用。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值