User account creation filtered due to spam.

Bug 52681 - [C++11] Using std::thread without -pthread crashes without warning
Summary: [C++11] Using std::thread without -pthread crashes without warning
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
Depends on:
Reported: 2012-03-23 01:49 UTC by Matt Kline
Modified: 2012-08-12 19:02 UTC (History)
0 users

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


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:

                      "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

	PR libstdc++/52681
	* src/c++11/ (thread::_M_start_thread): Improve error text
	when threads are not enabled.

Comment 8 Jonathan Wakely 2012-08-12 19:02:45 UTC
fixed for 4.8