30

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?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217

4 Answers4

42

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

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    Most Linux systems use glibc, some (like modern Debian and its derivatives) use EGLIBC. A few other system use other alternates. – SO Stinks Apr 03 '13 at 12:59
  • 1
    [glibc adds support with version 2.28](https://sourceware.org/bugzilla/show_bug.cgi?id=14092) – WorldSEnder Aug 02 '18 at 21:39
  • 1
    "modern debian uses EGLIBC" To clarify, Debian and it's derivatives used eglibc for releases 6 and 7 (circa 2009-2015) but switched to glibc in debian 8. As of early 2020 (Debian 10), Debian continues to use glibc. The original comment was posted in 2013, so it was, of course, correct at the time – asky Jan 07 '20 at 19:19
9

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

Tomas Pruzina
  • 8,397
  • 6
  • 26
  • 39
  • 1
    August 27, 2014 (2 years and 7 months later): no plans on developing `threads.h` in GLIBC yet. – Peque Aug 27 '14 at 06:48
  • Yep, seems that I was wrong unfortunatedly (so native windows threads / posix threads have to do for the forseeable future). For some reason nobody is pushing for support of portable threads :/ – Tomas Pruzina Aug 28 '14 at 22:14
  • 1
    [glibc 2.28, August 2018](https://sourceware.org/bugzilla/show_bug.cgi?id=14092) (mentioned in my answer). – Keith Thompson Aug 02 '18 at 22:54
6

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

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

user2548688
  • 103
  • 1
  • 4