C11 GCC threads.h 没有找到?

问题描述 投票:0回答:5

以下代码

#include <threads.h>

给我这个错误:

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

使用最新的 GCC 和 Clang 以及 -std=c11。

GCC 和 Clang 不支持 C11 线程吗?或者是否有黑客(或安装的东西)来获得它?我只是将 Ubuntu 14.04 与 Ubuntu 存储库中的 gcc 和 clang 软件包一起使用。

c multithreading c11
5个回答
28
投票

gcc
文档C11 status表示不支持线程,上面写着:

线程[可选] |图书馆问题(未实施)

如文档所示,这实际上不是

gcc
clang
问题,而是
glibc
问题。正如 Zack 指出的那样,看起来可能很快就会开展工作以获得对此的支持
glibc
,但现在这对您没有帮助。 您可以同时使用this

针对 glibc 2.28 修复

根据错误 14092 - 支持 C11 线程这在 glibc 2.28 中得到修复:

上游实施者:

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

2.28.会收录


6
投票

Musl 支持 C11

<threads.h>
.

在Debian中安装

musl-tools
,然后用
musl-gcc
编译。我正在使用 Musl 而不是 Glibc 引导 Debian。

另见this.


1
投票

虽然C11线程还没有实现,但C++11线程已经实现了,它们有类似的功能。当然,C++11 可能是一个不可接受的解决方案,在这种情况下,之前关于 POSIX 线程的评论是您最好的希望。


1
投票

线程已合并到主线 Glibc 中,例如在我的 Ubuntu 20.04 上可用。不幸的是,我似乎没有该功能的任何手册页。但这有效:

#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);
}

并测试它:

% 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

0
投票

可以用命令编译;

clang c-prog-with-threads_h.c

现在不使用-lpthread。

clang 版本 14.0.1,平台:Termux 应用程序,Android。

© www.soinside.com 2019 - 2024. All rights reserved.