2

//test.cpp

#include<iostream>
#include<thread>
using namespace std;
void call()
{
    cout<<"hello world"<<endl;
}
int main()
{
    thread t(call);
    t.join();
    return 0;
}

g++-4.7 -std=c++11 test.cpp -o test -pthread

The above compiled perfectly, but when I run ./test. I just get an error message that says "pure virtual method called , terminate called without an active exception"

Can anyone help me? Thanks!

Andy
  • 14,427
  • 3
  • 52
  • 76
Jimmy Lee
  • 21
  • 2
  • 1
    No repro with g++ 4.7.1. – kennytm Aug 09 '12 at 07:54
  • I have this problem on clang3.1! I thought it was a problem with that compiler, but apparently it has to do with something on libstdc++. I just switched back to gcc though, couldn't solve it. – mfontanini Aug 09 '12 at 11:14
  • 1
    Works fine with g++ 4.7.0 too. – Morwenn Aug 09 '12 at 11:55
  • The only way I can reproduce the error with this code is to remove `t.join()`. I know in some real-world code, I had to add a check to `std::thread::joinable()` before I called `std::thread::join()`. Might want to give that a shot. – Nathan Ernst Nov 29 '12 at 16:17

1 Answers1

4

It is a known bug. see http://llvm.org/bugs/show_bug.cgi?id=12730.

Nobody tried to fix it, sadly.

Community
  • 1
  • 1
Changming Sun
  • 857
  • 2
  • 7
  • 19
  • 1
    The bug report you linked to suggests defining the following macros: `__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1`, `__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2`, `__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4`, and `__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8`. It worked for me. – svetianov Apr 29 '13 at 00:54