minglei yin
minglei yin

Reputation: 55

How to understand the synchronization order which is a total order?

In JLS17.4.4, it says

Every execution has a synchronization order. A synchronization order is a total order over all of the synchronization actions of an execution.

I know what is a total order. And I know synchronization actions need to be ordered. But what confused me here is why it is a total order for ALL of the synchronization actions. For example, an unlock action on monitor m happens-before all subsequent lock actions on m. But if simultaneously, there is a lock action on monitor n, does it need to be ordered after the unlock action on monitor m? If not, then why can all of the synchronization actions have a total order? Thank you.

Upvotes: 5

Views: 1573

Answers (1)

user2982130
user2982130

Reputation:

Great question. While I don't know the answer for sure, the explanation that I believe best fits my understanding is yes, there is a "synchronization order" between two different locks, because JLS 17.4.2 defines the actions described to be ordered by "synchronization order" as having to be inter-thread (and thus one thread must have dispatched the action before another receives it).

However, here's the catch:

Synchronization actions induce the synchronized-with relation on actions, defined as follows [...]

Only actions ordered by synchronized-with produce the required/desired memory effects (i.e. advances the cache state).

So basically although all your "synchronization actions" follow the "synchronization order," (and thus "total order), but only those synchronized-with patterns are valid for thread-safety.

Upvotes: 2

Related Questions