gcc -pthread

Mark Andrews Mark_Andrews at isc.org
Thu Jun 15 14:34:45 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
> 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
>       application; and
> 
>    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
> built.
> 
> 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.
> >
> >       -pthread
> >              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".
> >
> > LIBRARY
> >     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.
> >
> > 	http://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/dads-
> pthread.html
> 
> 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
> libthr, etc).
> 
> -- 
> DE

	Thanks.

	From this I take it the best way to build a threaded library
	under FreeBSD is to not specify a threads library when
	specifying -shared to gcc.

	Hopefully someone will take this and add a page to the
	handbook which explains all this.

	Mark
--
Mark Andrews, ISC
1 Seymour St., Dundas Valley, NSW 2117, Australia
PHONE: +61 2 9871 4742                 INTERNET: Mark_Andrews at isc.org


More information about the freebsd-stable mailing list