0
我很震惊和困惑,说实话。在阅读“pthread_create导致分段错误”几十个主题后,我仍然有这个问题。我做了所有的命令,结果为零:在调试阶段,程序在线创建过程中给出了分段错误。Pthread_create导致分段错误(C++,Kubutnu 15)
我已经不明白什么。我会很感激你的帮助。提前致谢。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <interfacemanager.h>
using namespace std;
InterfaceManager IFACE;
void* watch_for_interfaces(void*)
{
IFACE.monitoring();
}
int main()
{
sigset_t *set;
sigemptyset(&(*set));
sigaddset(&(*set), SIGINT);
sigaddset(&(*set), SIGTERM);
pthread_t th1, th2;
int i=1;
if (pthread_create(&th2, NULL, watch_for_interfaces, (void*)&i) != 0) {
perror("th2 error\n");
exit(1);
}
printf ("Terminating\nPress eny key\n");
getchar();
exit(EXIT_SUCCESS);
return 0;
}
事情解决,有必要:1)不要混用C++与C头文件。如果使用gcc编译,C++头文件函数定义将无法使用,因为编译器不会使函数名称“变形”。如果使用g ++编译,C头文件函数定义将不可用,因为编译器会使函数名称“变形”。 – user3629249
更多需要解决的问题:2)该行:'exit(EXIT_SUCCESS);'会退出程序,所以'return 0;'语句将无法访问的代码。 3)如果使用gcc编译,#include将无法找到该文件。 4)在函数'watch_for_interfaces()'中a)建议使用'骆驼大小写'而不是下划线来分隔字b)通过调用'pthread_exit()'退出线程,而不是通过掉落函数的末尾 –
user3629249
更多的东西修复:5)main()应该调用'pthread_join(th2);'优雅地等待线程退出,然后退出main()。 6)强烈建议使用gcc进行编译,并删除#include声明 –
user3629249