为什么这个简单的c++11线程示例在使用clang 3.2编译时失败?

Why does this simple c++11 threading-example fail, when compiled with clang 3.2?

本文关键字:clang 编译 失败 简单 c++11 线程 为什么      更新时间:2023-10-16

我不知道我做错了什么。这个很短的程序:

#include <iostream>
#include <string>
#include <atomic>
#include <thread>
using namespace std;
int
main(int argc, char ** argv)
{
        thread foo( []() { 
                cout << "Hello World" << endl;
                return 0; 
        } );
        foo.join();
        return 0;
}

使用gcc (4.7.2)编译时效果很好:

 $ g++ -ggdb -std=c++11 -pthread -o clang_thread_test clang_thread_test.cpp 
 $ ./clang_thread_test 
Hello World

但是,当使用clang (3.2; x86_64-pc-linux-gnu; thread model: posix)编译时,它执行失败:

 $ clang++ -ggdb -std=c++11 -pthread -o clang_thread_test clang_thread_test.cpp 
 $ ./clang_thread_test 
pure virtual method called
terminate called without an active exception
Aborted

有一个已知的原因吗?我发现的唯一的事情是与缺失的-pthread switch或未使用的libc++有关。据我所知,后者只适用于苹果系统。

确认此操作有效:

clang++ --std=c++11 -pthread -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53841

修复:

linux error: no matching constructor for initialization of 'duration' 

使用建议修复

- const chrono::nanoseconds __delta = __atime - __c_entry;
- const __clock_t::time_point __s_atime = __s_entry
+ __delta; + const auto __delta = __atime - __c_entry;
+ const auto __s_atime = __s_entry
+ __delta; in file condition_variable