Subject: Re: Threading problems
To: Arto Huusko <arto.huusko@utu.fi>
From: Nathan J. Williams <nathanw@wasabisystems.com>
List: tech-kern
Date: 11/24/2004 23:22:15
Arto Huusko <arto.huusko@utu.fi> writes:

> Why only pthread_create()? I mean, how can a module be written
> so that it can satisfy thread-safety in the presence of a
> threaded app, without the module directly using pthread functions
> such as mutexes etc.
> 
> Say a module has some global list, and functions add_to_list()
> and remove_from_list(). How can the module be written so that
> it magically is thread safe when the app is threaded?  To work
> with threaded apps correctly, it clearly needs to use
> pthread mutexes, and that in turn requires that the module is
> linked against pthread.

The trick is to call a function which is a no-op when libpthread isn't
linked in, but is the appropriate mutex operatoin when it is (if the
program isn't actually threaded, then you don't have any thread
concurrency issues, and thus no-ops are safe). Libc manages this with
a set of aliases that have default, no-op implementations in libc but
that are overridden when libpthread is linked in. The proposal is to
make this libc trick extend to all pthread-using apps.

        - Nathan