#include <iostream>
#include <string>
#include <functional>
#include <windows.h>
#include <thread>
#include <future>
// passes value by reference
int func2(int & a)
{
Sleep(10000);
++a;
return a;
}
// passes value by reference
int func3(int & a)
{
a *= 2 ;
return a;
}
int main()
{
int d = 2;
thread t8(func2, ref(d)); // 2 + 1 = 3
thread t9(func3, ref(d)); // 2 * 2 = 4
//t9.join();
cout << "the value of d from func3 is " << d << endl; //should print 4
//t8.join();
cout << "the value of d from func2 is " << d << endl; // should print 3
}
Pedantic note: You're not simulating a race condition, because one actually exists (well, you're trying to make one exist, at least), if we're dealing with actual threads. Because it's actually happening. Sleeps can help contrive a race condition easier when debugging.