C11 <thread.h> in GCC?

Dervin Thunk picture Dervin Thunk · Jan 14, 2012 · Viewed 23.7k times · Source

I’m trying to compile some C11 code using thread.h, but I can’t. I've recompiled GCC (running 4.6.2 now), and I’m trying to compile with gcc -std=c1x file.c -o file. I can do this in g++ (using the thread library, that is) but I can’t in C. Is thread.h not included in the GCC distribution yet?

Answer

Keith Thompson picture Keith Thompson · Jan 14, 2012

The standard C11 header for threading is <threads.h>, not <thread.h>. See section 7.26 of the N1570 draft.

Most of the C standard library, including stdio for example, is not included in the gcc distribution. Instead, gcc depends on whatever runtime library is provided by the operating system. That generally includes both the headers (like <threads.h>) and the actual code that implements the library.

For most Linux systems (or GNU/Linux if you prefer), the library is GNU's glibc; for other systems it will be something else.

So the real question is probably when glibc, or whichever C library you're using, will support C11's threading features.

glibc adds support for C11 threads in version 2.28. Ubuntu 18.04.1 LTS system currently still uses glibc 2.27. Again, this applies only to implementations using GNU libc, not to all gcc-based implementations. Mentioned by WorldSEnder in a comment.

UPDATE: Ubuntu 18.10 (not an LTS (Long Term Support) release) has glibc 2.28, which supports <threads.h>. Also, as user2548688's answer points out, the musl C library supports <threads.h>. On Ubuntu, you can install the musl-dev package and use the musl-gcc command.

(Note that a few parts of the library, those most closely tied to the compiler, are provided by gcc itself. The threading library probably isn't one of them, but certainly some compiler support is required.)