Reading after critical region

57 Views Asked by At

I have an int num = 2 protected by a std::mutex mut. I write over num in one thread and then read num in a second thread.

// writer thread
mut.lock();
num = 5;
mut.unlock(); 
// reader thread
mut.lock();
mut.unlock();
int num_copy = num;

If the mut.unlock() in the writer thread executes before the mut.lock() in the reader thread, will num_copy always equal 5?

0

There are 0 best solutions below