Segmentation fault (core dumped) 谁知道怎么回事。

talkdream 2016-06-17 04:07:29

下面代码中 如果是#if 1 ,程序运行起来会出cor dumped,如标题。
如果是#if 0 正常创建线程没有问题,谁知道是什么原因。


#if 1
bt_pthread_create();
#else
pthread_t id;
int ret;
ret = pthread_create(id, NULL, (void*)tasksub, NULL);
#endif

完整代码
====================================================================
#include "stdio.h"
#include "bt_if_lib.h"
#include <pthread.h>
extern void tasksub(void);
void bt_pthread_create(void);

void bt_pthread_create(void)
{
pthread_t id;
int ret;
ret = pthread_create(id, NULL, (void*)tasksub, NULL);
}

void main(void)
{
int i;

printf("HMI Main START\n");
I_bt_init();

sleep(10);
#if 1 // 问题位置,这样就会Coredump #if 0 就没有问题,函数里面代码和else中是一样的,拷贝过去的。
bt_pthread_create();
#else
pthread_t id;
int ret;
ret = pthread_create(id, NULL, (void*)tasksub, NULL);
#endif
for(i=0;i<50;i++)
{
I_bt_audio_play();
sleep(5);
}
while(1) //T.B.D

{

printf("HMI Main While\n");

sleep(100);

}
}
==================================================================================
...全文
306 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
brookmill 2016-06-20
  • 打赏
  • 举报
回复
用错了参数,编译器应该有警告的吧。 编译器的警告,有很多是没用的,但也有很多真的是帮你找到了问题,一定要重视。
brookmill 2016-06-18
  • 打赏
  • 举报
回复
$ man pthread_create ...... int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 所以pthread_create的第一个参数应该是&id而不是id

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧