简体   繁体   English

C11 <thread.h> 在GCC?

[英]C11 <thread.h> in GCC?

I'm trying to compile some C11 code using thread.h , but I can't. 我正在尝试使用thread.h编译一些C11代码,但我不能。 I've recompiled GCC (running 4.6.2 now), and I'm trying to compile with gcc -std=c1x file.c -o file . 我已经重新编译了GCC(现在运行4.6.2),我正在尝试使用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? 我可以用g ++(使用thread库)来做到这一点,但我不能在C中。是否thread.h没有包含在GCC发行版中的thread.h

The standard C11 header for threading is <threads.h> , not <thread.h> . 用于线程的标准C11头是<threads.h> ,而不是<thread.h> See section 7.26 of the N1570 draft . N1570草案第7.26节。

Most of the C standard library, including stdio for example, is not included in the gcc distribution. 大多数C标准库(例如stdio)都不包含在gcc发行版中。 Instead, gcc depends on whatever runtime library is provided by the operating system. 相反,gcc依赖于操作系统提供的任何运行时库。 That generally includes both the headers (like <threads.h> ) and the actual code that implements the library. 这通常包括头文件(如<threads.h> )和实现库的实际代码。

For most Linux systems (or GNU/Linux if you prefer), the library is GNU's glibc; 对于大多数Linux系统(如果你愿意,还是GNU / Linux),库是GNU的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,或者你正在使用的C库,都支持C11的线程功能。

glibc adds support for C11 threads in version 2.28 . glibc在版本2.28中添加了对C11线程的支持 Ubuntu 18.04.1 LTS system currently still uses glibc 2.27. Ubuntu 18.04.1 LTS系统目前仍使用glibc 2.27。 Again, this applies only to implementations using GNU libc, not to all gcc-based implementations. 同样,这仅适用于使用GNU libc的实现,而不适用于所有基于gcc的实现。 Mentioned by WorldSEnder in a comment. WorldSEnder在评论中提到过。

UPDATE: Ubuntu 18.10 (not an LTS (Long Term Support) release) has glibc 2.28, which supports <threads.h> . 更新:Ubuntu 18.10(不是LTS(长期支持)版本)有glibc 2.28,它支持<threads.h> Also, as user2548688's answer points out, the musl C library supports <threads.h> . 另外,正如user2548688的回答所指出的那样, musl C库支持<threads.h> On Ubuntu, you can install the musl-dev package and use the musl-gcc command. 在Ubuntu上,您可以安装musl-dev包并使用musl-gcc命令。

(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.) (注意,库中的一些部分,与编译器关系最密切的部分, 由gcc本身提供的。线程库可能不是其中之一,但肯定需要一些编译器支持。)

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. ...(Atomics - stdatomic.h - 是可选的,可能需要等待在GCC 4.8中实现相关的语言功能。我猜测附件中的threads.h和bounds-checking接口中的可选线程接口K现在不想用于glibc,尽管它们可能会进入单独的库中。

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). 我的猜测是我们不会在相当长的一段时间内看到这个,至少在标准的glibc和gcc中没有实现(源文章提供了一些见解)。 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 多数民众赞成2k14(断言(生存_2012)):P

To quote from the GCC standards page about C11: 引用GCC 标准页面关于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. 虽然我只有GCC 4.6.1,但我的系统上没有“thread.h”头文件。

Neither the changes pages for 4.6 nor 4.7 mentions threads. 4.64.7的更改页面都没有提到线程。


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. 4.7更改页面中提到了“线程”,但在C11上下文中似乎没有任何与它有关的内容。 Also, nothing about C11 is mentioned in the upcomming 4.8 page. 另外,在upcomming 4.8页面中没有提到C11。

musl libc有C11 http://www.musl-libc.org/ glibc还没有threads.h支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM