-pthread propagation

Dmitry Marakasov amdmi3 at amdmi3.ru
Wed Apr 1 16:18:39 PDT 2009


I have a question about -pthread. Imagine the situation where one port
installs shared library that uses threads, and other port links with
this library. A question: should the second port explicitely add
-pthread to linker flags?

For example, graphics/ilmbase is built with pthread support by default,
but it's shared libraries are not linked with -pthread:

% ldd /usr/local/lib/libIlmThread.so
	libIex.so.6 => /usr/local/lib/libIex.so.6 (0x2819c000)
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x28300000)
	libm.so.5 => /lib/libm.so.5 (0x281ad000)
	libc.so.7 => /lib/libc.so.7 (0x2808b000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x281c6000)

no libthr.so mention. Thus,

% gcc helloworld.cc -lIlmThread -L/usr/local/lib
/usr/local/lib/libIlmThread.so: undefined reference to `pthread_create'

I assume this should be fixed in ilmbase instead of all dependent ports
(for example, graphics/nvidia-texture-tools and graphics/devil, which
supports the former), am I right? Btw, libIlmThread.la _does_ have
-pthread in dependency_libs.

However, I've encountered situations where linking with library
linked with -pthread will succeed, but the resulting binary will not
be broken. Example is games/battletanks:

When built as is (note that it has ${PTHREAD_LIBS} explicitely added to
LDFLAGS), it runs without problems. However, if you remove
${PTHREAD_LIBS}, it'll still build successfully, but won't run:

% bt 
[03:04:45.449][src/main.cpp:44]	 [notice] starting up... version: 5800 beta
[03:04:45.449][src/main.cpp:46]	 [notice] mem avail: -1 mb
terminate called after throwing an instance of '__gnu_cxx::__concurrence_lock_error'
  what():  __gnu_cxx::__concurrence_lock_error
[1]    58620 abort (core dumped)  bt

I think that's linked with static variable initialization - it should
be protected with a mutex in threaded environment, but it doesn't happen
correctly when linking without -pthread, even if with -lthr.

I'll be really grateful if someone explains what really happens when
using -lpthread, and what happens in the above mentioned error case
(cc'ing hackers@).

So should -pthread be forced in ldflags
1) Only in ports that explicitely use threads
2) In all ports that link with -lthr implicitely, including through
other ports?

Dmitry Marakasov   .   55B5 0596 FF1E 8D84 5F56  9510 D35A 80DD F9D2 F77D
amdmi3 at amdmi3.ru  ..:  jabber: amdmi3 at jabber.ru    http://www.amdmi3.ru

More information about the freebsd-ports mailing list