clang c11 threads.h未找到

我试图在xcode中设置一个c11线程示例…但它似乎没有threads.h标头,虽然它没有抱怨这里描述的宏:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

__STDC_NO_THREADS__整数常量1,用于指示实现不支持标头。

显示方言显示错误

看起来几乎没有什么能支持C11中的线程function…也许我会尝试将它变成clang …

我的机器上的clang(ubuntu / linux上的v.3.2)没有定义function测试宏。 支持该function将需要C库中的支持,这通常不是编译器附带的。 所以基本上clang的答案与gcc没什么不同,它们通常建立在相同的C库上,即glibc,请参阅此处获取gcc的答案 。

它似乎没有threads.h标题,虽然它没有抱怨

C11有2个关于__STDC_NO_THREADS__规格

7.26线程
定义宏__STDC_NO_THREADS__不需要提供此标头也不需要支持其任何工具。 C11N1570§7.26.12

__STDC_NO_THREADS__整数常量1,用于指示实现不支持头。 C11N1570§6.10.8.31

根据§7.26.12:

 #ifdef __STDC_NO_THREADS__ #error "No threading support" #else #include  #endif 

根据§6.10.8.3:

 #if defined(__STDC_NO_THREADS) && __STDC_NO_THREADS__ == 1 #error "No threading support" #else #include  #endif // Certainly this can be simplified to #if defined(__STDC_NO_THREADS) && __STDC_NO_THREADS__ 

或者每个#if中使用的未定义常量的值是多少? 至

 #if __STDC_NO_THREADS__ 

这符合OP的代码,所以我希望能够使用兼容的C11编译器。


然而,看起来OP有@Kevin的解决方案 。 这可能是一个错误的解决方案,因为__STDC_NO_THREADS看起来像一个拼写错误(缺少尾随__ )。

 #if !defined(__STDC_NO_THREADS) || __STDC_NO_THREADS__ 

在C ++ 11中,你想要#include ,而不是threads.h

 #include  #include  void fun() { std::cout << "fun!" << std::endl; } int main() { std::thread t ( fun ); t.join (); return 0; }