【问题标题】:C11 GCC threads.h not found?未找到 C11 GCC 线程.h?
【发布时间】:2014-05-17 12:28:07
【问题描述】:

以下代码

#include <threads.h>

给我这个错误:

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

使用最新的 GCC 和带有 -std=c11 的 Clang。

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

【问题讨论】:

  • 完全支持 c11 和/或 threads.h 的编译器数量为负数。 :(
  • C11 线程,当它们最终被支持时,将由 C library 支持,而不是编译器本身。今年夏天,至少有两个人在谈论将 glibc 的 C11 线程作为 GSoC 项目实现。与此同时,您可能会发现 pthread.h API 非常相似,尽管所有内容的名称都更长。
  • 许多标准化正在“祝福”现有的被广泛实施的扩展,这被认为是一个好主意,但经验表明,从 C 标准中出现的新功能到实际被广泛实施之间的前置时间编译器/库更像是“10 年到永远”而不是“最多 3 年”。
  • @Emmet /me 一言不发地把威士忌酒瓶滑下吧台

标签: c multithreading c11


【解决方案1】:

gcc 文档C11 status 表示它不支持线程,它说:

线程 [可选] |库问题(未实现)

正如文档所示,这实际上不是gccclang 问题,而是glibc 问题。正如 Zack 指出的那样,there may be work under way soon 似乎可以在glibc 中获得对此的支持,但现在这对你没有帮助。 你可以同时使用this

为 glibc 2.28 修复

根据Bug 14092 - Support C11 threads,这是在 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 中。

【讨论】:

  • 谢谢。那么GCC和Clang都不支持C11线程吗?然后我将不得不回归到 POSIX 线程:-(
  • @lucasart 看起来是这样
  • 我希望我昨天读过这篇文章。 The Bugzilla for threading in glibc 仍然开放(2 年以上)。 @lucasart:POSIX 线程似乎几乎不是回归;几乎相同的界面,“便携”,实现。 :) 事实上,针对glibc 2.19 编译报告__STDC_NO_THREADS__1。另请参阅this SO 以获取其他链接。它是 pthreads(或 C++11 std::thread,顺便说一句,这取决于 -pthread...)。
  • 显然glibc side上有些动静
  • 我现在可以使用 Glibc 2.31 (Ubuntu 20.04) #include &lt;threads.h&gt;
【解决方案2】:

虽然 C11 线程尚未实现,但 C++11 线程已经实现并且它们具有类似的功能。当然,C++11 可能是一个不可接受的解决方案,在这种情况下,之前关于 POSIX 线程的 cmets 是你最大的希望。

【讨论】:

    【解决方案3】:

    Musl支持C11&lt;threads.h&gt;

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

    另见this

    【讨论】:

    • 在 Ubuntu 中,链接头 c11threads.h 会导致一堆错误。例如。 PTHREAD_MUTEX_TIMED_NP 未声明。搜索说它应该在包含的pthreads.h 中,但似乎不是。
    • @Hi-Angel 您需要使用 -std=gnu11 编译器标志而不是 -std=c11 或者需要在包含 c11threads.h 文件之前使用#define _GNU_SOURCE。
    • 2021 年 2 月 28 日星期日适用于 debian 9.13。
    【解决方案4】:

    线程已合并到主线 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2023-03-26
      • 2017-07-15
      相关资源
      最近更新 更多