1

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.

1 Answers1

1

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.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64