我写了一个程序来创建10个线程并正常运行。该程序运行良好,但最终会出现分段错误。这是什么故障,是什么原因造成的,我该如何解决? 我的代码是:程序执行期间的分段错误
#include<stdio.h>
#include<pthread.h>
void *print(void *num);
int main()
{
pthread_t tid[10];
int n,check;
void *exitstatus;
for(n=1;n<=10;n++)
{
check=pthread_create(&tid[n],NULL,print,(void *)&n);
if(check=0)
printf("thread created");
pthread_join(tid[n],&exitstatus);
}
return 0;
}
void *print(void *num)
{
int i,*val=(int *)num;
for(i=0;i<(5);i++)
printf("Hello World!!!(thread %d)\n",*val);
}
您是否尝试过使用gdb来隔离错误的来源? – ziu 2013-03-21 12:41:57