Dec 15, 2011 at 8:44pm UTC
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 Dec 15, 2011 at 8:45pm UTC
Dec 15, 2011 at 9:16pm UTC
If you're using GCC, this is an error printed when you forget to link with -lpthread
Dec 15, 2011 at 10:22pm UTC
Ugh, feeling pretty dumb right now! Thanks! :)
Dec 15, 2011 at 10:34pm UTC
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 Dec 15, 2011 at 10:43pm UTC