11

I am trying to setup a c11 thread example in xcode... but it doesn't seem to have the threads.h header, though it isn't complaning about the macro described here:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

__STDC_NO_THREADS__The integer constant 1, intended to indicate that the implementation does not support the <threads.h> header.

showing dialect showing error

Morwenn
  • 21,684
  • 12
  • 93
  • 152
Grady Player
  • 14,399
  • 2
  • 48
  • 76

5 Answers5

3

Looks like almost nothing supports the threads feature in C11... maybe I will try to get it into clang...

Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • That might be simpler if you are willing to build upon a different C library. I recently looked into musl (http://www.musl-libc.org/), and there such a task seems to be doable. If you would be satisfied with a wrapper around POSIX, my P99 (p99.gforge.inria.fr) could be sufficient. – Jens Gustedt Apr 26 '13 at 21:37
  • I haven't ever looked at any alternatives (I will now)... it will need to be in glibc / gcc / clang eventually... if it is only a wrapper for posix threads where they are supported... then I think that would suffice. – Grady Player Apr 26 '13 at 21:46
  • `threads.h` now works on Linux (since glibc 2.28, musl 1.1.5), and FreeBSD (link with `-lstdthreads`). Still no mainstream support on macOS or Windows though. But now that it is supported on both Linux and FreeBSD, the odds that macOS and Windows will join the C11 threads party is higher. Actually, the Pelles C compiler on Windows already does support it, but I wouldn't exactly consider that "mainstream support" (which would be MSVC and/or MinGW-w64) – Simon Kissane Jan 16 '23 at 23:44
  • @simonKissane, at least `__STDC_NO_THREADS__` is defined now... so I guess it is at least compliant. – Grady Player Jan 17 '23 at 03:59
1

With the clang on my machine (v. 3.2 on ubuntu/linux) that feature test macro isn't defined. Support for that feature will need support in the C library, that usually doesn't come with the compiler. So basically the answer for clang will not be much different than for gcc, they usually build upon the same C library, namely glibc, see here for answer for gcc.

Community
  • 1
  • 1
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1

Just in case anyone is looking this up in 2021+, Apple still doesn't support this, and likely never will. As others have stated, pthreads is by far the best bet. Note that widely, C11 threads are not supported. I would go as far as to say pthreads is more portable in most circumstances.

From a development perspective, C11 threads are much too limited, and obfuscate user space vs. kernel space implementation features, along with implementation attributes.

If you really need C11 threads I would recommend doing one of three things.

  1. Don't.
  2. Use a cross-platform library: https://github.com/tinycthread/tinycthread seems to do a good job.
  3. Write your own polyfill. The library I just mentioned tries to support a bunch of platforms (though it's inactive now), but anyone with a little bit of experience with pthreads and win32 threads will have no trouble (but, maybe a headache) writing a polyfill. I actually did this the other day out of interest, and it took just a few hundred lines, not too bad. For any project that will use it alot, this will give more control over usage.

The good thing to note is some other C11 features are supported on OSX, like C11 Atomics, which complementing would be impossible without a lot of ASM knowledge.

Hunter Kohler
  • 1,885
  • 1
  • 18
  • 23
  • 1
    Yes... it was naive of the committee to release thread support as late as 2011 and think it would be implemented, when de facto standards like pthreads were already established more than 10 years earlier. I don't know the reason why they didn't make pthreads a standard C lib, but I very much doubt the reasons were technical. More likely related to politics and prestige. – Lundin Dec 20 '21 at 09:03
0

it doesn't seem to have the threads.h header, though it isn't complaining

C11 has 2 specs about __STDC_NO_THREADS__

7.26 Threads
Implementations that define the macro __STDC_NO_THREADS__ need not provide this header nor support any of its facilities. C11 N1570 §7.26.1 2

__STDC_NO_THREADS__ The integer constant 1, intended to indicate that the implementation does not support the <threads.h> header. C11 N1570 §6.10.8.3 1

Per §7.26.1 2:

#ifdef __STDC_NO_THREADS__
#error "No threading support"
#else
#include <threads.h>
#endif

Per §6.10.8.3:

#if defined(__STDC_NO_THREADS) && __STDC_NO_THREADS__ == 1
#error "No threading support"
#else
#include <threads.h>
#endif

// Certainly this can be simplified to
#if defined(__STDC_NO_THREADS) && __STDC_NO_THREADS__

or per What is the value of an undefined constant used in #if? to

#if __STDC_NO_THREADS__

This matches OP's code, so I would have expected that to work with a compliant C11 compiler.


Yet it looks like OP has a solution per @Kevin. That may be a false solution as __STDC_NO_THREADS looks like a typo (missing trailing __).

#if !defined(__STDC_NO_THREADS) || __STDC_NO_THREADS__
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-19

In C++11, you want to #include <thread>, not threads.h

#include <iostream>
#include <thread>

void fun() { std::cout << "fun!" << std::endl; }

int main() {
    std::thread t ( fun );
    t.join ();
    return 0;
}
Marshall Clow
  • 15,972
  • 2
  • 29
  • 45