简体   繁体   English

C11 GCC threads.h 未找到?

[英]C11 GCC threads.h not found?

The following code以下代码

#include <threads.h>

Gives me this error:给我这个错误:

fatal error: threads.h: No such file or directory

Using the latest GCC and Clang with -std=c11.使用最新的 GCC 和 Clang 和 -std=c11。

Is C11 threading not supported by GCC and Clang? GCC 和 Clang 不支持 C11 线程吗? Or is there a hack (or something to install) to get it?或者是否有黑客(或要安装的东西)来获取它? I'm just using Ubuntu 14.04 with the gcc and clang packages from the Ubuntu repo.我只是将 Ubuntu 14.04 与 Ubuntu 存储库中的 gcc 和 clang 包一起使用。

The gcc document C11 status indicates that it does not support threading, it says: gcc文档C11 状态表明它不支持线程,它说:

Threading [Optional] |穿线 [可选] | Library issue (not implemented)图书馆问题(未实施)

As the document indicates this is not really a gcc or clang issue but glibc issue.正如文档所指出的,这不是真正的gccclang问题,而是glibc问题。 As Zack pointed out it looks like there may be work under way soon to get support for this into glibc but that won't help you now.正如 Zack 指出的那样,似乎很快就会有工作正在进行中,以将其支持到glibc但这对您现在无济于事。 You can use this in the meantime.您可以在此期间使用

Fixed for glibc 2.28已针对 glibc 2.28 修复

According the Bug 14092 - Support C11 threads this is fixed in glibc 2.28:根据Bug 14092 - 支持 C11 线程,这已在 glibc 2.28 中修复:

Implemented upstream by:上游实施:

9d0a979 Add manual documentation for threads.h 9d0a979 为threads.h 添加手动文档
0a07288 nptl: Add test cases for ISO C11 threads 0a07288 nptl:为 ISO C11 线程添加测试用例
c6dd669 nptl: Add abilist symbols for C11 threads c6dd669 nptl:为 C11 线程添加 abilist 符号
78d4013 nptl: Add C11 threads tss_* functions 78d4013 nptl:添加 C11 线程 tss_* 函数
918311a nptl: Add C11 threads cnd_* functions 918311a nptl:添加 C11 线程 cnd_* 函数
3c20a67 nptl: Add C11 threads call_once functions 3c20a67 nptl:添加 C11 线程 call_once 函数
18d59c1 nptl: Add C11 threads mtx_* functions 18d59c1 nptl:添加 C11 线程 mtx_* 函数
ce7528f nptl: Add C11 threads thrd_* functions ce7528f nptl:添加 C11 线程 thrd_* 函数

It will be included in 2.28.它将包含在 2.28 中。

Musl support C11 <threads.h> . Musl支持 C11 <threads.h>

In Debian install musl-tools , and then compile with musl-gcc .在 Debian 中安装musl-tools ,然后使用musl-gcc I am working on bootstrapping Debian with Musl instead of Glibc.我正在使用 Musl 而不是 Glibc 来引导 Debian。

Also see this .也看到 这个

While C11 threads has not been implemented yet, C++11 threads have been implemented and they have similar functionality.虽然 C11 线程尚未实现,但 C++11 线程已经实现并且它们具有类似的功能。 Of course, C++11 may be an unacceptable solution, in which case the prior comments about POSIX threads are your best hope.当然,C++11 可能是一个不可接受的解决方案,在这种情况下,关于 POSIX 线程的先前评论是您最大的希望。

Threads have been merged into mainline Glibc and are available for example on my Ubuntu 20.04.线程已合并到主线 Glibc 中,例如在我的 Ubuntu 20.04 上可用。 Unfortunately I don't seem to have any manual pages for the function.不幸的是,我似乎没有该功能的任何手册页。 But this works:但这有效:

#include <threads.h>
#include <stdio.h>

int hello_from_threading(void *arg) {
    printf("Thread about to take a nap...\n");
    thrd_sleep(&(struct timespec) { .tv_sec = 3 }, NULL);
    printf("Woke up from 3 second slumber\n");
    return 42;
}

int main(void) {
    thrd_t thread;
    thrd_create(&thread, hello_from_threading, NULL);
    int res;
    printf("A thread was started\n");
    thrd_join(thread, &res);
    printf("Thread ended, returning %d\n", res);
}

and testing it:并测试它:

% gcc threading.c -o threading -lpthread
% ./threading
A thread was started
Thread about to take a nap...
Woke up from 3 second slumber
Thread ended, returning 42

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

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