多线程程序中的核心转储

发布于 2024-08-11 01:34:18 字数 686 浏览 1 评论 0原文

我试图编写一个简单的多线程程序。它正在倾倒核心。我的函数在下面创建线程时被调用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-08-18 01:34:19

这行是错误的:

printf("Thread %d starting...\n",*tid);

您可以通过以下方式实现您想要的:

printf("Thread %d starting...\n",(int) t);


printf("线程 %d 开始...\n", tid);

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);

浸婚纱 2024-08-18 01:34:19

从这一行开始:

printf("thread %d is sleeping...

所有对 tid 的引用都应该是 *tid

Beginning with this line:

printf("thread %d is sleeping...

All of the references to tid should be *tid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文