exit & thread safety

Jakub Jelinek jakub@redhat.com
Tue Jun 4 10:16:00 GMT 2002


Hi!

We got a report in Red Hat bugzilla about:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/resource.h>
#include <unistd.h>

void* foo(void * n)
{
  exit(0);
}

int main()
{
  int n;
  int status;
  void *thread_result;
  size_t size;
  pthread_t thread_id;
  pthread_attr_t thread_attr;
  char * val;
  status = pthread_create(&thread_id, NULL, foo, &n);
  if (status) printf("ERROR: creating thread\n");
  status = pthread_create(&thread_id, NULL, foo, &n);
  if (status) printf("ERROR: creating thread\n");
}

crashing from time to time.
http://www.opengroup.org/onlinepubs/007908799/xsh/_exit.html
says:
If exit() is called more than once, the effects are undefined.
among other things, so I'm not sure if glibc needs to do anything at all,
but I think we can crash in other ways too (like if one threads does
dlclose and another exit() or if one thread does atexit/__cxa_atexit/...
and another one exit() at the same time.
If exit() e.g. did not free the memory (which is needed for
memory usage debuggers, right?), did not even change anything in the
__exit_funcs chain and used compare_and_swap for each f->flavor right
before its handler is called, then we should be ok IMHO.

What do you think?

	Jakub



More information about the Libc-hacker mailing list