I want to create a multi-threaded C++ program using boost. What I want to do, is to call a function Alpha
, which sleeps, and prints out some messages. Whilst that function is being processed, i.e. sleeping, then I want my main program to continue. So, I want Alpha
to run in the background, without interrupting the main
function.
Here is my code:
void Alpha()
{
std::cout << "Starting sleep." << std::endl;
sleep(5);
std::cout << "Sleep over." << std::endl;
}
int main()
{
std::cout << "A" << std::endl;
boost::thread threadAlpha(Alpha);
threadAlpha.join();
std::cout << "B" << std::endl;
}
The output I get is:
A
Starting sleep.
Sleep over.
B
However, I want the thread to run in the background, such that it does not cause the main loop to wait. Therefore, I was expecting the output to be:
A
Starting sleep.
B
Sleep over.
Or possibly:
A
B
Starting sleep.
Sleep over.
Why is this not behaving as I would expect?