clang c11 threads.h not found

2019-04-06 04:46发布

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

4条回答
地球回转人心会变
2楼-- · 2019-04-06 04:55

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

查看更多
Evening l夕情丶
3楼-- · 2019-04-06 04:56

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.

查看更多
【Aperson】
4楼-- · 2019-04-06 04:56

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;
}
查看更多
虎瘦雄心在
5楼-- · 2019-04-06 04:57

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__
查看更多
登录 后发表回答