31

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.

Is C11 threading not supported by GCC and Clang? 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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
lucasart
  • 383
  • 1
  • 5
  • 11
  • 4
    The number of compilers fully supporting c11 and/or threads.h is negative. :( – this Apr 05 '14 at 01:34
  • 3
    C11 threads, when they eventually become supported, will be supported by the C *library* and not the compiler proper. At least two people are talking about implementing C11 threads for glibc as a GSoC project this summer. In the meantime, you may find the `pthread.h` API suspiciously similar, albeit with much longer names for everything. – zwol Apr 05 '14 at 01:40
  • In RHEL, there is no `threads.h` but there is `pthread.h`. In solaris there is `thread.h`. – alvits Apr 05 '14 at 01:44
  • @Zack any references? This was the only [glibc thread](https://sourceware.org/bugzilla/show_bug.cgi?id=14092) I could find. – Shafik Yaghmour Apr 05 '14 at 01:51
  • @ShafikYaghmour e.g. https://sourceware.org/ml/libc-alpha/2014-03/msg00451.html – zwol Apr 05 '14 at 01:52
  • @Zack thank you, added the link into my answer. – Shafik Yaghmour Apr 05 '14 at 01:55
  • 5
    A lot of standardization is “blessing” existing widely-implemented extensions that are considered a good idea, but experience shows that the lead time between a new feature appearing in the C standard and actually being widely implemented by compilers/libraries is more like “10 years to never” than “up to 3 years”. – Emmet Apr 05 '14 at 01:58
  • 8
    @Emmet */me wordlessly slides the bottle of whiskey down the bar toward you* – zwol Apr 05 '14 at 02:14
  • Possible duplicate of [C11 in GCC?](http://stackoverflow.com/questions/8859394/c11-thread-h-in-gcc) – Ciro Santilli OurBigBook.com Nov 05 '15 at 10:03
  • 1
    See also [Does any C library implement C11 threads for GNU/Linux?](http://stackoverflow.com/q/24557728/827263) – Keith Thompson Jan 08 '16 at 01:49

5 Answers5

28

The gcc document C11 status indicates that it does not support threading, it says:

Threading [Optional] | Library issue (not implemented)

As the document indicates this is not really a gcc or clang issue but glibc issue. 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. You can use this in the meantime.

Fixed for glibc 2.28

According the Bug 14092 - Support C11 threads this is fixed in glibc 2.28:

Implemented upstream by:

9d0a979 Add manual documentation for threads.h
0a07288 nptl: Add test cases for ISO C11 threads
c6dd669 nptl: Add abilist symbols for C11 threads
78d4013 nptl: Add C11 threads tss_* functions
918311a nptl: Add C11 threads cnd_* functions
3c20a67 nptl: Add C11 threads call_once functions
18d59c1 nptl: Add C11 threads mtx_* functions
ce7528f nptl: Add C11 threads thrd_* functions

It will be included in 2.28.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Thank you. So neither GCC nor Clang support C11 threading then? I will have to regress back to POSIX threads then :-( – lucasart Apr 05 '14 at 01:50
  • @lucasart looks to be the case – Shafik Yaghmour Apr 05 '14 at 01:55
  • 1
    I wish I had read this yesterday. [The Bugzilla for threading in `glibc`](https://sourceware.org/bugzilla/show_bug.cgi?id=14092) is still open (2+ years). @lucasart : POSIX threads seems hardly a regression; nearly identical interface, "portable", *and* implemented. :) Indeed, compiling against [`glibc` 2.19](http://www.gnu.org/software/libc/libc.html) reports `__STDC_NO_THREADS__` is `1`. See also [this SO](http://stackoverflow.com/questions/8859394/c11-thread-h-in-gcc) for additional links. pthreads it is (or C++11 `std::thread`, which btw depends on `-pthread`...). – hoc_age Jul 02 '14 at 15:17
  • Apparently some movement on the [glibc side](https://sourceware.org/ml/libc-alpha/2015-06/msg00894.html) – Shafik Yaghmour Jun 29 '15 at 14:30
  • I can `#include ` now with Glibc 2.31 (Ubuntu 20.04) – Antti Haapala -- Слава Україні Jan 17 '21 at 10:05
6

Musl support C11 <threads.h>.

In Debian install musl-tools, and then compile with musl-gcc. I am working on bootstrapping Debian with Musl instead of Glibc.

Also see this.

Jeff Hammond
  • 5,374
  • 3
  • 28
  • 45
user2548688
  • 103
  • 1
  • 4
  • In Ubuntu the linked header *c11threads.h* causes a bunch of errors. E.g. `PTHREAD_MUTEX_TIMED_NP` being undeclared. Searching says it should be in the included `pthreads.h`, but it seems isn't. – Hi-Angel Jul 06 '15 at 04:47
  • 1
    @Hi-Angel You either need to use the -std=gnu11 compiler flag instead of -std=c11 or you need to use a #define _GNU_SOURCE before including that c11threads.h file. – nos Jan 15 '16 at 10:29
  • Works on debian 9.13 Sunday, February 28 2021. – zkutch Feb 28 '21 at 04:49
1

While C11 threads has not been implemented yet, C++11 threads have been implemented and they have similar functionality. Of course, C++11 may be an unacceptable solution, in which case the prior comments about POSIX threads are your best hope.

Jeff Hammond
  • 5,374
  • 3
  • 28
  • 45
1

Threads have been merged into mainline Glibc and are available for example on my 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
  • 3
    Is there a reason why it is necessary to link to pthreads to actually compile the code when the intention is to use standard threads from C11? – dandev486 Mar 05 '22 at 23:18
-1

You can compile it with the command;

clang c-prog-with-threads_h.c

without using -lpthread now. (And latest versions of compilers did not need to specify std=c11, because of it is default).

clang version 14.0.1, platform: Termux app, Android.

yildiz
  • 1
  • 2