Bug 52681 - [C++11] Using std::thread without -pthread crashes without warning
Summary: [C++11] Using std::thread without -pthread crashes without warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 enhancement
Target Milestone: 4.8.0
Assignee: Jonathan Wakely
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-23 01:49 UTC by Matt Kline
Modified: 2012-08-12 19:02 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-04-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Kline 2012-03-23 01:49:21 UTC
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?
Comment 1 Jonathan Wakely 2012-03-23 12:31:38 UTC
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.
Comment 2 Matt Kline 2012-03-23 13:16:23 UTC
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.
Comment 3 Paolo Carlini 2012-03-23 13:29:38 UTC
Probably, something in the docs could be also useful.
Comment 4 Jonathan Wakely 2012-04-18 10:05:43 UTC
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)
Comment 5 Matt Kline 2012-04-18 14:04:37 UTC
That, if feasible, would be perfect.
Comment 6 Jonathan Wakely 2012-04-18 14:16:20 UTC
Entirely feasible, and probably safe enough for the 4.6 and 4.7 branches too.
Comment 7 Jonathan Wakely 2012-08-12 18:57:58 UTC
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
Comment 8 Jonathan Wakely 2012-08-12 19:02:45 UTC
fixed for 4.8