core dump in a multithread program
i was trying to write a simple multithreaded program. It is dumping the core. I have my function that is called when a thread is created below:
void *BusyWork(void *t)
{
int i;
int *tid;
int result=0;
tid = t;
printf("Thread %d starting...\n",*tid);
for (i=0; i<10; i++)
{
result = result + (i* i);
}
printf("thread %d is sleeping for %d sec's\n",tid,tid);
sleep(tid);
printf("Thread %d done. Result = %e\n",tid, result);
pthread_exit((void*) t);
}
My call to pthread_create function is below inside the main function:
int t;
for(t=0; t<NUM_THREADS; t++)
{
printf("Main: creatin开发者_JS百科g thread %d\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);
}
Please help me in finding where is the problem? Thanks in advance.
This line is wrong:
printf("Thread %d starting...\n",*tid);
You can achieve what you want with:
printf("Thread %d starting...\n",(int) t);
or printf("Thread %d starting...\n", tid);
Beginning with this line:
printf("thread %d is sleeping...
All of the references to tid
should be *tid
.
精彩评论