calling std::thread throws std::system_error

I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <thread>

void free_fun()
{
  std::cout << __func__ << " running on " << std::this_thread::get_id() << std::endl;
}

int main()
{
  try
  {
    std::thread t(&free_fun); // this line throws
    t.join();
  } 
  catch (std::system_error &er)
  {
    std::cout << "error - " << er.what() << std::endl;
  }
  return 0;
}


The call to std::thread constructor throws

error - Operation not permitted


Am I using std::thread wrong? I'm confused as to what is failing here.




Last edited on
If you're using GCC, this is an error printed when you forget to link with -lpthread
Ugh, feeling pretty dumb right now! Thanks! :)
Don't be, it's a GCC quirk. The same program runs compiled by clang-3.0 on linux without any special options.
You're gonna hit another GCC quirk in a moment: it may require -D_GLIBCXX_USE_NANOSLEEP if you want to use std::this_thread::sleep_for()
Last edited on
Topic archived. No new replies allowed.