8

here is a very minimal C++11 Thread API code that I am trying to compile

#include<iostream>
#include<thread>

using namespace std;

void threadFunction(void)
{
    cout<<"hello from thread:";//<<this_thread::get_id()<<endl;
}

int main()
{
    std::thread t(threadFunction);
    t.join();

    return 0;
}



On Compiling this as
g++ thread1.cpp -pthread -std=c++11
I get the following error
pure virtual method called
terminate called without an active exception
Aborted


What wrong, can someone please help
Note that I am compiling this on Beaglebone Black with ARM A8 processor

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Anjanu
  • 623
  • 2
  • 8
  • 19
  • Sorry, but your code works just fine in VS2012, with default compiler settings. So the code is not the problem. – Valdrinium May 10 '14 at 16:16
  • What is your OS and compiler versions? – Massa May 10 '14 at 16:19
  • Maybe you're compiling command is wrong? It's weird. I've tried it here (http://www.compileonline.com/compile_cpp11_online.php) and it worked. (compile cmd "g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1") – Iosif Murariu May 10 '14 at 16:27
  • This is on Beaglebone Black, which runs Angstrom Linux. Also I am using putty to ssh into my beaglebone via usb – Anjanu May 10 '14 at 16:32
  • losif Marariu tried your compile command, same error – Anjanu May 10 '14 at 16:35
  • Possible duplication http://stackoverflow.com/questions/16171401/why-does-this-simple-c11-threading-example-fail-when-compiled-with-clang-3-2 – tofi9 May 10 '14 at 16:53
  • It also works fine with GNU C++ 4.7.2 on my Linux Debian (amd64). – nickie May 10 '14 at 17:17
  • @Scooby That's not how undefined behavior works (undecidability and all). But in this particular case the code really is perfectly acceptable. I run into a different problem with g++ 4.8.1 which is described [here](https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1228201). – Voo May 10 '14 at 17:35
  • what is your stl library? – Ran Regev May 11 '14 at 06:12

3 Answers3

6

This is a bug in either libstdc++ or Clang, depending on who you ask. It should work if you are using a version of Clang released after October 2013. What do you see when you run g++ --version?

As a workaround, you could try using this command line instead. I don't guarantee that it would work; please post a comment with your results.

g++ -pthread -std=c++11 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_{1,2,4} thread1.cpp

Here's the bug report:

http://llvm.org/bugs/show_bug.cgi?id=12730

And here's the official fix to the Clang driver:

https://llvm.org/viewvc/llvm-project?view=revision&revision=191707

I don't know if this was also previously a bug in the GCC driver, and/or whether it's been fixed.

Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
4

Ran into the same problem on a Raspberry Pi 2 Model B with an ARM Cortex-A7. Compiled with g++-4.7, turned out the culprit was a compiler flag:

-march=armv7-a
Tim
  • 91
  • 2
  • 7
1

Although clang had a related issue, this is entirely a gcc bug now recorded at: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62100

pabigot
  • 989
  • 7
  • 8
  • And in fact it's an OpenEmbedded bug. A better workaround until it's fixed is to use `-mcpu=cortex-a8`. See the [OE Core thread](http://www.mail-archive.com/openembedded-core@lists.openembedded.org/msg55490.html) for context. – pabigot Aug 14 '14 at 14:08