Check out this java code snippet:
Lock l = ReentrantLock();
l.lock();
try {
counter++;
} finally {
l.unlock();
}
This code guarantees that only one thread will execute the code in the try block in a particular time.
My question is: how the lock/unlock mechanism guarantees memory visibility between the threads? (what is the mechanism that will make sure that the counter result will be flushed into the main memory by Thread X, and will be loaded from the main memory by Thread Y that will come afterward?)