Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C11 <thread.h> in GCC?

Tags:

c

gcc

c11

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?

like image 488
Dervin Thunk Avatar asked Jan 14 '12 01:01

Dervin Thunk


2 Answers

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.)

like image 185
Keith Thompson Avatar answered Sep 24 '22 02:09

Keith Thompson


Further information about this can be found here.

... (Atomics - stdatomic.h - are optional, and will probably need to wait for associated language features to be implemented in GCC 4.8. I'd guess that the optional threading interfaces in threads.h and bounds-checking interfaces in Annex K aren't wanted for glibc for now, although they could potentially go in separate libraries.

My guess is that we won't see this implemented for quite some time, at least not in standard glibc and gcc (sourced post provides some insight). My personal guess is something like one year, it will take probably something like 2 years until it will be stable enough for production use. Thats 2k14 (assert(survival_2012)) :P

like image 20
Tomas Pruzina Avatar answered Sep 24 '22 02:09

Tomas Pruzina