Security barrier and java.util.concurrent.locks.Condition example

I have a question regarding memory barriers when using the Condition provided by Lock .

As for the example provided in javadoc for Condition , I have a usage question:

 int putptr, takeptr, count; 

Should these attributes be declared unstable? As I understand from the example, the thread may not see modifications, for example, count .

Or, what, when signal() is called, all modifications made since the acquisition of the lock are visible to other threads? How is the code in a synchronized block?

If so, are the modifications visible when calling signal() or when unlock() is called in a lock?

Thanks.

Edit: I see in javadoc Lock :

All lock implementations must comply with the same memory synchronization semantics as the built-in monitor lock, as described in Section 17.4 of the Java ™ Language Specifications:

  • A successful lock operation has the same memory synchronization effects as a successful lock action.
  • A successful unlock operation has the same memory synchronization effects as a successful unlock action.

Failed lock and unlock operations and repeated lock / unlock operations do not require any memory synchronization effects.

They mean: “A successful lock operation has the same memory synchronization effects as entering a synchronized block,” and “A successful unlock operation has the same memory synchronization effects as exiting a synchronized block”?

+7
source share
1 answer

As you should read, all entries that occur before lock.unlock are visible to all subsequent lock.lock . The thread that await ing when it woke up would essentially be lock.lock . Thus, all entries that have occurred since the previous unlock will now be visible.

signal has no memory semantics since your last status point or when unlock() is called on the lock correct.

They mean: “A successful lock operation has the same memory synchronization effects when entering a synchronized block” and “A successful unlock operation has the same memory synchronization effects as an output from a synchronized block”?

Yes exactly! More specifically, the compiler will issue commands to the monitor control unit and monitorexit.

+7
source

All Articles