3

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

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

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

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.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
mageta
  • 677
  • 2
  • 8
  • 12
  • 3
    I've had the same [problem](http://llvm.org/bugs/show_bug.cgi?id=12730). It's been fixed a couple of weeks ago. – mfontanini Apr 23 '13 at 14:03
  • I have a similar solved question. [Look this](http://stackoverflow.com/questions/15445222/vector-of-stdthreads/15446434#15446434) – awesoon Apr 23 '13 at 14:04
  • 1
    Reading over the fix, it is possible that compiling with `__GCC_HAVE_SYNC_COMPARE_AND_SWAP_` defined *might* help (but it might not). – Yakk - Adam Nevraumont Apr 23 '13 at 14:17
  • It certainly hides an apparent Clang miscompilation of the code in question (if I can believe Chip's input on the bug report)... – rubenvb Apr 23 '13 at 14:38
  • @rubenvb It looks to me like the problem is that `__GCC_HAVE_SYNC_COMPARE_AND_SWAP_` results in ABI changes, and so the libstdc++ binaries, compiled with that macro, are incompatible with clang's inlined code produced without that macro. – bames53 Apr 23 '13 at 15:18
  • ah, so I'm for once not the failure :D.. If I define the mentioned macros before including anything in the given program, the code compiles and works with clang. But as I have no idea what else this gcc-internal macro might do I wouldn't consider this a good fix. – mageta Apr 23 '13 at 16:40
  • @mageta it's a good fix; clang needs to mimic GCC here, and that means defining the macro. The intrinsic is implemented in both compilers anyway. – rubenvb Apr 23 '13 at 17:41

1 Answers1

9

Confirmed that this works:

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 thread bug fix for:

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

using the Suggested fix:

- 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 
virtualPN
  • 936
  • 1
  • 6
  • 8