Ok I'm learning multi-threads in c++11 using Mac. As far as I know that all threads are executed simultaneously. I found the following code from here
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
void foo()
{
std::cout << "\nIn foo \n";
}
void bar(int x)
{
std::cout << "\nIn bar \n";
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
Every time I run the code, I get weird results as the following sample
m
aIIinnn ,bf aofroo o
and bar now execute concurrently... foo and bar completed.
what am I missing?