Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

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

To quote from the GCC standards page about C11:

GCC has limited incomplete support for parts of this standard

While I only have GCC 4.6.1, I do not have a "thread.h" header file anywhere on my system.

Neither the changes pages for 4.6 nor 4.7 mentions threads.


There are "threads" mentioned in the 4.7 changes page, but nothing that seems to have anything to do with it in a C11 context. Also, nothing about C11 is mentioned in the upcomming 4.8 page.

musl libc has C11 http://www.musl-libc.org/ glibc doesn't threads.h support yet

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top