找不到C11 GCC threads.h?

时间:2014-04-05 01:33:30

标签: c multithreading c11

以下代码

#include <threads.h>

给我这个错误:

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

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

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

4 个答案:

答案 0 :(得分:20)

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.8中修复:

  

通过以下方式实施上游:

     

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_ * functions

     

它将包含在2.28中。

答案 1 :(得分:3)

Musl支持C11 <threads.h>

在Debian中安装musl-tools,然后使用musl-gcc进行编译。我正在与Musl而不是Glibc一起开始使用Debian。

另见this

答案 2 :(得分:1)

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

答案 3 :(得分:0)

线程已合并到主线 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
相关问题