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.put(c);
}
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");
}
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.