[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using a multithreaded library

From: Neal H. Walfield
Subject: Using a multithreaded library
Date: 29 Sep 2002 21:35:52 -0400
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/21.2

Julien PUYDT has brought this interesting scenario to my attention.
He also wrote the supplied test program.

It is possible that a program can end up using pthreads without
actually linking to them directly.  For instance, programs which use
dlopen and family to load DSOs may not be multithreaded themselves,
however, the DSO may be.  In this case, the DSO can end up crashing
the application (a program demonstrating this is attached).

Although it is easy to make pthreads capable of detecting this case
and then "upgrading" unknown threads to pthreads, we have to change
__hurd_threadvar_stack_mask and __hurd_threadvar_stack_offset.  The
result is that the main thread's TSD is no longer accessible and the
program soon crashes.  Would it be possible to special case the main
thread in a similar way as the signal thread (of course, how to we
even get the stack bounds?).  Perhaps there is something else that we
can do.

This is a bit hacky and I am not even sure that POSIX says that we
have to support this feature (although the description of
pthread_atfork alludes to something similar).  Either way, I cannot
find any wording that might preclude it.  As a side note, linuxthreads
covers this case.


Test program:

There are two files: test.c and test-lib.c.  Compile using:

        gcc -ldl -o test test.c
        gcc -shared -fPIC -lpthread -o test-lib.so test-lib.c


#include <dlfcn.h>
#include <stdio.h>

  void *handle = NULL;
  int (*plugin_function)(void);

  handle = dlopen ("./test-lib.so", RTLD_LAZY);
  plugin_function = (int (*)(void)) dlsym (handle, "externally_called");

  if((*plugin_function) () == 0)



#include <pthread.h>

void *
loop (void *dummy)
  while (1)
    sched_yield ();

  pthread_t pth;

  pthread_create (&pth, 0, loop, 0);

  return 0;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]