stefan
stefan

Reputation: 10345

Link Time Optimization conflicting with multithreading support

As I've read about improved link time optimization support in g++-4.9, I want to give it a try. Sadly, I get exceptions at run time, specifically std::system_errorwith e.what() == Enable multithreading to use std::thread: Operation not permitted.

Now I normally know how to fix that error: add -pthread to my compiler invokation, but in fact, I already have this parameter!

My sample code is:

#include <thread>

int main()
{
   std::thread t([](){}); // do nothing in a thread!
   t.join();              // wait for nothing to be done
}

Compiled with (X being 7, 8 or 9)

g++-4.X -std=c++11 -pthread test.cpp -o thread_test_fine

works flawlessly as expected, no runtime error.

However,

g++-4.X -std=c++11 -pthread -flto test.cpp -o thread_test_runtime_error

fails with the system_error exception.

Question:

Is this behavior intended (what's the fix?) or is it a bug?

(Before this question may come up: my compilers are all build with --enable-threads=posix)

Upvotes: 3

Views: 753

Answers (1)

qehgt
qehgt

Reputation: 2990

Try to add this parameters:

-Wl,--no-as-needed

If it helps then it's a gcc bug: https://stackoverflow.com/a/19463892/280758

Upvotes: 2

Related Questions