I'm having some thread programming learning on my virtual machine. The code that do not perform as expected is following:
#include <iostream>
#include <thread>
using namespace std;
void function01() {
for (int i=0; i<100; i++) {
std::cout << "from t1:" << i << std::endl;
}
}
int main() {
// data race and mutex
std::thread t1( function01 );
for (int i=0; i<100; i++) {
std::cout << "from main:" << i << std::endl;
}
t1.join();
return 0;
}
These code should make a data race on std output. But when I compiled it with
:!g++ -std=c++11 -pthread ./foo.cpp
and running, every time I got a result in which 100 times "t1" followed 100 times "main". What confusing me is that when I did the same thing on my another ubuntu14.04 which was installed in my old lap-top, the code performed as my expected. That means this code encountered with data race.
I don't know much about vmware. Are the threads running on the vmware are managed and won't encountered data race?
------------- second edit -----------------------
Thanks for everybody.
The quantity of core might be the main reason. And I had my expected result after setting quantity of vm core to more than one.