21149

Why do I get a runtime error for this simple thread example?

Question:

#include <iostream> #include <thread> int main() { std::thread th([] { std::cout << "Hello, World\n"; }); th.join(); }

This is all I have and it causes a runtime error. Why is that? I'm using GCC 4.8 (Ideone).

Answer1:

The error from ideone is:

<blockquote>

terminate called after throwing an instance of 'std::system_error'<br /> what(): Enable multithreading to use std::thread: Operation not permitted

</blockquote>

Which means you need to be compiling with -pthread as already suggested by @Praetorian.

The code runs fine in Visual Studio 2012.

Recommend

  • Cartesian power (a special Cartesian product) — choose elements from array, in repeatable style
  • output of fmod function c++
  • Matching quote contents
  • Get the sizeof Object's Members
  • java is not working in linux. Why?
  • Making a comparator from an ordered container
  • Vector of comma separated token to const char**
  • Ruby on Rails: Cant download new gems (Name or service not known)
  • Implementing a function that perfect-forwards to std::thread
  • python string manipulation
  • doxygen generated documentation with auto-generated links to qt project
  • Custom thread pool supporting async actions
  • Why a class has only one destructor? [duplicate]
  • Cannot terminate threads
  • sscanf string splitting not working
  • How does `std::terminate` know to treat `std::exception`s specially?
  • C++ String tokenisation from 3D .obj files
  • How do I compile a C/C++ program through windows command prompt?
  • runtime error when linking ffmpeg libraries in qt creator
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • pickle.PicklingError: args[0] from __newobj__ args has the wrong class with hadoop python
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Using $compile in a directive triggers AngularJS infinite digest error
  • Detecting null parameter in preprocessor macro
  • configure: error: no acceptable C compiler found in $PATH
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Django: Count of Group Elements
  • How to check if every primary key value is being referenced as foreign key in another table
  • Where to put my custom functions in Wordpress?
  • How to handle AllServersUnavailable Exception
  • How to get next/previous record number?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Linker errors when using intrinsic function via function pointer
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to get Windows thread pool to call class member function?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass