Linux + uClibc + pthread (C):应用程序在最后冻结

发布于 2024-12-19 11:52:40 字数 1232 浏览 2 评论 0原文

我正在尝试构建自己的 uClibc 嵌入式系统。我遇到了一些问题,但两天后,我解决了所有问题。只剩下一个:pthread 无法正常工作。 这是来自 python 的配置脚本的简单程序:

#include <pthread.h>

void* routine(void* p){return NULL;}

int main(){
  pthread_t p;
  if(pthread_create(&p,NULL,routine,NULL)!=0)
    return 1;
  (void)pthread_detach(p);
  return 0;
}

在我的基于 glibc 的系统上,它运行然后退出。 但在我基于 uclibc 的系统上,它运行、结束线程并冻结:

[Thread debugging using libthread_db enabled]
[New Thread 0x801 (LWP 17631)]
[New Thread 0x402 (LWP 17632)]
[Thread 0x402 (LWP 17632) exited]
[Thread 0x801 (LWP 17631) exited]
^C
Program received signal SIGINT, Interrupt.
0xb7f768e7 in sigsuspend () from /lib/libc.so.0

我尝试了新旧 linuxthreads,但它们都不起作用。你有主意吗?

编辑:

好的,我发现了更多信息:

#include <pthread.h>
#include <stdio.h>
void* routine(void* p){printf("AAA!\n");return NULL;}

int main(){
  pthread_t p;
  pthread_create(&p,NULL,&routine,NULL);
   printf("BBB!");
  (void)pthread_detach(p);
  pthread_exit(0);
  exit(0);
}

仅打印“AAA!”,然后冻结(glibc 系统以随机顺序打印“AAA!”和“BBB!”)。所以我认为uclibc pthreads本身一定存在一些错误。还有其他帮助吗? 尝试了其他一些 pthread 测试,每个测试最终都冻结了。 编辑:我不知道为什么会发生这种情况,但我复制了预编译的 uclibc 并且它现在可以工作了。

I'm trying to build my own uClibc embedded system. I encountered some problems, but after 2 days, I solved all of them. Only one is remaining: pthread doesnt work correctly.
Here is simple program, from python's configure script:

#include <pthread.h>

void* routine(void* p){return NULL;}

int main(){
  pthread_t p;
  if(pthread_create(&p,NULL,routine,NULL)!=0)
    return 1;
  (void)pthread_detach(p);
  return 0;
}

On my glibc-based system, it runs and then exits.
But on my uclibc-based system, it runs, ends threads and freezes:

[Thread debugging using libthread_db enabled]
[New Thread 0x801 (LWP 17631)]
[New Thread 0x402 (LWP 17632)]
[Thread 0x402 (LWP 17632) exited]
[Thread 0x801 (LWP 17631) exited]
^C
Program received signal SIGINT, Interrupt.
0xb7f768e7 in sigsuspend () from /lib/libc.so.0

I tried both old and new linuxthreads, none of them worked. Do you have an idea?

Edit:

OK, I found some more info:

#include <pthread.h>
#include <stdio.h>
void* routine(void* p){printf("AAA!\n");return NULL;}

int main(){
  pthread_t p;
  pthread_create(&p,NULL,&routine,NULL);
   printf("BBB!");
  (void)pthread_detach(p);
  pthread_exit(0);
  exit(0);
}

prints only "AAA!", then freezes (glibc system prints both "AAA!" and "BBB!" in random order). So I think there must be some error in uclibc pthreads itself. Any other help?
Tried some other pthread tests and each of them freezes in the end.
Edit: I didnt find out why is this hapenning, but I copied precompiled uclibc and it works now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

箜明 2024-12-26 11:52:40

如果您没有通过其他方式确保主线程是最后一个运行的线程,则 main 需要在返回之前调用 pthread_exit(),以等待所有其他线程终止。

如果没有其他线程在运行,则变为无操作,因此无论如何调用它都没有坏处。

If you have not ensured by another means that the main thread is the last one running, main needs to call pthread_exit() before returning, to wait for all other threads to terminate.

If there are no other threads running, that becomes a no-op, so there is no harm in calling it anyway.

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