【发布时间】:2014-04-13 19:26:38
【问题描述】:
我的程序中有分段错误错误。我正在使用 POSIX C 中的多线程程序进行练习。我在 FREEBSD 系统中运行这些程序。
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#define NUM_THREADS 15
void *PrintHello(void *threadid)
{
printf("Hello World from thread: %p \n", threadid);
fflush(NULL);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int rtrn, *i;
pthread_t threads[NUM_THREADS];
for((*i)=0; (*i)<NUM_THREADS; (*i)++)
{
if( pthread_create(&threads[(*i)], NULL, PrintHello, (void *)i) != 0)
{
printf("Error code %d detected while thread %d was being created.", rtrn, *i);
}
}
return 0;
}
我不得不将 i 整数变量转换为指针整数,因为这解决了编译期间的错误(第 22 行:警告:从不同大小的整数转换为指针)
编译错误已解决,但现在当我运行程序时,它显示:Segmentation fault: 11 (core dumped)
可能之前的警告还没有解决,但我不知道如何解决它......
【问题讨论】:
-
你永远不会将
i初始化为合理的值。
标签: c multithreading pointers posix void-pointers