为什么这个简单的c ++ 11线程示例失败,编译时使用clang 3.2? [英] Why does this simple c++11 threading-example fail, when compiled with clang 3.2?

查看:246
本文介绍了为什么这个简单的c ++ 11线程示例失败,编译时使用clang 3.2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我在这里做错了什么。这个非常短的程序:

I can't figure out what I'm doing wrong here. This very short program:

#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;
}

2):

It works perfectly when compiled with 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;线程模型:posix)无法执行:

However, when compiled with clang (3.2; x86_64-pc-linux-gnu; thread model: posix) it fails to execute:

 $ 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开关或不使用的libc ++ 。 。据我所知,后者仅适用于苹果系统的相关

Is there a known reason for this? The only things I found were related to a missing -pthread switch or a not used libc++. To my knowledge the latter is only relevant on apple-systems.

推荐答案

确认这作品:

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

chrono线程错误修正:

chrono thread bug fix for:

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 

这篇关于为什么这个简单的c ++ 11线程示例失败,编译时使用clang 3.2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆