3

to build a c11 thread program with glibc I still need link with -lpthread,why? glibc 2.28 claims c11 thread support but why I need pthread still?

musl can build c11 thread without pthread though.

Shawn Shaw
  • 181
  • 3
  • 8

1 Answers1

4

Yes, glibc's C11 threads implementation is using pthreads library underneath and that's why it needs linking with pthread library. In glibc, pthreads is (was always) a separate library - thus the need to link it.

Whereas in musl library, the thread implementation is a part of the main C library itself and thus there's no need to link any thread library whether you use pthreads or C11 threads with musl. Also, see https://www.openwall.com/lists/musl/2012/07/25/3.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • based on this thread https://sourceware.org/bugzilla/show_bug.cgi?id=14092 it seems c11 thread was added independent of pthread since 2.28, but it is not. – Shawn Shaw Aug 12 '20 at 11:42
  • 1
    I don't think that bugzilla claims that C11 threads is a separate/independent implementation - only some discussion about using internal types. In any case, you can see the [glibc code](https://sourceware.org/git/?p=glibc.git;a=tree;f=sysdeps/pthread;hb=bad4a908ff90ca999217ea91571c221afdd5b2a3) (see the `thrd_*` implementations) that they share the same [internal implementation](https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_create.c;hb=bad4a908ff90ca999217ea91571c221afdd5b2a3#l611). – P.P Aug 12 '20 at 11:59
  • you're right, glibc just added a wrapper on top of its NPTL pthread code to implement c11 thread. for musl, it is similar, though musl has its own pthread implementation. – Shawn Shaw Aug 13 '20 at 04:10