多线程程序中的核心转储
我试图编写一个简单的多线程程序。它正在倾倒核心。我的函数在下面创建线程时被调用:
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);
}
我对 pthread_create 函数的调用位于主函数内部:
int t;
for(t=0; t<NUM_THREADS; t++)
{
printf("Main: creating thread %d\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);
}
请帮助我找到问题出在哪里? 提前致谢。
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: creating 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这行是错误的:
您可以通过以下方式实现您想要的:
或
printf("线程 %d 开始...\n", tid);
This line is wrong:
You can achieve what you want with:
or
printf("Thread %d starting...\n", tid);
从这一行开始:
所有对
tid
的引用都应该是*tid
。Beginning with this line:
All of the references to
tid
should be*tid
.