Without -pthread, the constructor of std::thread always throws std::system_error. While this was apparently the resolution to bug 42198, simply throwing an error with the code 'operation_not_permitted' is rather mystifying to someone who isn't aware of the problem in the first place. Would it be possible to offer a more descriptive error, preferably at compile time?
The problem is that not all targets need -pthread, or some which do need it spell it differently, and not all platforms define _REENTRANT when the option is used, so there's no reliable way to do what you're asking for.
Would it be possible to at least make the exception that is being thrown more descriptive? I had guessed that some kind of compile-time warning might be impossible, but my main concern is that the exception that gets thrown offers little insight on what the problem might be.
Probably, something in the docs could be also useful.
We could fix this just by overloading __throw_system_error to take a string: __throw_system_error(int(errc::operation_not_permitted), "Enable multithreading support to use std::thread"); then passing that to the std::system_error constructor. That would give something like: terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading support to use std::thread: Operation not permitted Aborted (core dumped)
That, if feasible, would be perfect.
Entirely feasible, and probably safe enough for the 4.6 and 4.7 branches too.
Author: redi Date: Sun Aug 12 18:57:53 2012 New Revision: 190330 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190330 Log: PR libstdc++/52681 * src/c++11/thread.cc (thread::_M_start_thread): Improve error text when threads are not enabled. Modified: trunk/libstdc++-v3/ChangeLog trunk/libstdc++-v3/src/c++11/thread.cc
fixed for 4.8