in my example thread 1 and 2 write to the same shared resource x.
and the main thread will read the value.
I want to have the result 24 i.e. thread2 then thread1.
how can I control that ? I tried to define thread 2 before 1 and print the result after join the two threads and the result was 24 but this result is not guaranteed since the threads work in parallel
int x = 10;
void mainThread1(){
x *= 2;
}
void mainThread2(){
x += 2;
}
int main() {
std::thread th1(mainThread1);
std::thread th2(mainThread2);
std::cout << x << std::endl; // random print 20 or 22
th1.join();
th2.join();
}