2009/6/19 Michael Matz <matz@suse.de>:
Now, for linux there's a bit magic involved. To make the cost for non thread-safe programs really low some routines in libc _are not thread-safe_. That's fine, those programs aren't linked against libpthread, so they have announced to be not interested in threads. libpthread.so provides thread-aware variants of some functions that are also provided by libc itself. For instance open() and friends is such function. The libpthread variant includes some locking for shared data structures.
(Apart from these overriding functions it also includes the usual pthread functions, pthread_*, e.g. pthread_spin_lock).
How this all works is because ELF symbols resolution rules say so. If an application calls "open" and is linked against libpthread and libc (which both provide a version of "open", one thread-aware the other not) then the dynamic linker will chose the first it finds. Other things (gcc .spec files) make sure that libpthread is linked in _before_ libc when -pthread is given. This can't be ensured for -lpthread, the latter being simply a request for a random other library, as far as the system knows. So only -pthread, not -lpthread, makes really sure that libpthread is linked at the right place vs. libc, so that it can override the non thread-aware functions of it.
Looking to some .pc (pkg-config) files I though about this... -pthread can make sure that libpthread is linked in before libc, ok. But what happens if I compile a library with -pthread and then use it from a program that doesn't uses libpthread? Wouldn't in this case the library use the symbols from libc? I though that this could be fixed if every library that uses libpthread adds -pthread to the CFlags in its .pc file (so the program will link against libpthread even if it doesn't uses it). But if I'm using --as-needed this would not work. You said: "Other things (gcc .spec files) make sure that libpthread is linked in _before_ libc when -pthread is given". But this doesn't solves the problem at runtime. There is also any special exception in ld.so so libpthread is always loaded before libc? Even if libpthread is only used by a library deep in the dependency tree and not in the main binary? -- To unsubscribe, e-mail: opensuse-packaging+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-packaging+help@opensuse.org