C11 <thread.h> in GCC?

孤者浪人 提交于 2019-11-27 04:04:32

问题


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?


回答1:


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




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/8859394/c11-thread-h-in-gcc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!