std::thread f()
{
void some_function(); // <- here
return std::thread(some_function);
}
std::thread g()
{
void some_other_function(int); // <- here
std::thread t(some_other_function,42);
return t;
}
views:
111answers:
3It's just a function declaration, as you thought. It is common (and recommended) to put function declarations in header files, but this is by no means required. They may be in function bodies.
Lines like:
void some_function();
simply declare a function which will be defined later. Functions don't necessarily have to be declared outside of function scope.
Define a function returning a thread
object:
std::thread f()
{
Declare an extern
function with no arguments returning void
(usually this is not done in local scope, but it is valid):
void some_function();
Start a thread executing that function, and return a handle to it:
return std::thread(some_function);
}
Same deal as before:
std::thread g()
{
void some_other_function(int);
But this is not valid. You can't make a copy of a thread, so technically it is not OK to make a local thread
object and then return it. I'd be surprised if this compiled, but if it does, it might break when you build for the debugger.
std::thread t(some_other_function,42);
return t;
}
This would work, though:
return std::move( t );