C++0x concurrency not work properly in Ubuntu in Virtualbox

Hello guys,

Source cod:

#include <future>
#include <mutex>
#include <iostream>
#include <string>

std::mutex printMutex; // enable synchronized output with print()

void print (const std::string& s)
//std::lock_guard<std::mutex> l(printMutex);
for (char c : s) {
std::cout << std::endl;

int main()
auto f1 = std::async (std::launch::async,
print, "Hello from a first thread");
auto f2 = std::async (std::launch::async,
print, "Hello from a second thread");
print("Hello from the main thread");

The lock_guard statement was commented.

On Windows 10, I compiled with cl.exe:

cl -nologo -EHsc -GR -Zc:forScope -Zc:wchar_t -Foobjs/mutex1.o -Feouts/mutex1 mutex1.cpp

run the mutex1:

HelHleo lfHlerolom tlh oef rmo mf aai rnso etcmhorne aad dt
fihrrseta dt

it is ok, output mixed.

I compiled and run it in Ubuntu 16 installed in VirtualBox, the output is:

Hello from the main thread
Hello from a first thread
Hello from a second thread

looks like the lock_guard statement enabled, but it is still commented.

I compiled and run it in Ubuntu 16 installed on physical machine, the output is same as cl.exe, it is ok.

I want to know, the reason is VirtualBox not implement Multithread properly?

I'm not really sure what kind of answer you're looking for. Without the lock guard the code contains a race condition. The order of execution of each thread is therefore undefined. I don't know why you would expect to see any particular patterns in the output.
Topic archived. No new replies allowed.