deischen at freebsd.org
Thu Jun 15 13:48:48 UTC 2006
On Thu, 15 Jun 2006, Mark Andrews wrote:
> Is there any good reason why "gcc -pthread" links in
> -lpthead except when -shared is specified?
Because one may want to build applications to use different
threading libraries. Application A may work better with libthr,
while application B prefers libpthread. Both applications may
also rely on libfoo which needs a threading library in order
to be MT-safe. If libfoo records a dependency on libpthread,
and application A uses libthr, then that won't work (crashy
I think we took the approach a long time ago that there
were basically 2 reasons for a library to require threading:
1) To be MT-safe when used in the presence of a threaded
2) To create threads behind the scenes in support of an
application (i.e., a hypothetical libaio).
In the case of 1, the library can use #pragma weak on
pthread_create and detect whether or not a threads
library is present and only use locking when pthread_create
!= NULL (libc provides stubs for all the pthread functions
necessary for locking). In the case of 2, applications
should know what libraries need threads and be required
to supply -lpthread (or -lthr, or -pthread) when they are
This may or may not change in the future because all
the UNIX world is Linux :( (And for possible problems
when using symbol versioning.)
> Also the gcc man page does not match reality.
> Link a user-threaded process against libc_r instead of libc.
> man pthread mentions these libraries but provided no discussion
> on if or when you should include them on the command line. Nor
> does it mention "-pthread".
> POSIX Threads Library (libpthread, -lpthread)
> 1:1 Threading Library (libthr, -lthr)
> Reentrant C Library (libc_r, -lc_r)
> The hacker handbook seems to indicate that you shouldn't need
> to use -lpthread or -lc_r.
That's the porters handbook and they prefer to use PTHREAD_LIBS so
one can override the default threading library when building the
port. PTHREAD_LIBS defaults to -pthread because -pthread does
the right thing on different archs where the default threading
library varies (x86, amd64, ia64 use libpthread, sparc64 uses
More information about the freebsd-stable