Whichever thread enters the new atomic block will never exit from the while loop because the thread that sets the appropriate flag never got the chance to run.
grose
Conceptually, another way to think about this is we have multiple pieces of code that are designed such that multiple of them need to start simultaneously, but locks are contrary to that notion
sanchuah
Replacing synchronized with atomic will cause both threads locking in the for loop. The memory operation result would be visible to other threads only after leaving the atomic block. Although thread 1 and thread 2 modify flagA and flagB to true in their blocks, they still cannot not see the updated value because the other thread has not left the atomic block.
solovoy
This is like forcing two sections of inter-dependent code to be mutually exclusive.
afa4
I think there will be a RW conflict detected between the two transactions and so they will be serialized. But since the two transactions are dependent on each other for their completion there will be a deadlock.
toutou
Synchronize() creates a lock to a piece of code. Other concurrent thread will see the result as soon as it executes. But the TM will not commit writing operation until the whole atomic part has been completed.
Whichever thread enters the new atomic block will never exit from the while loop because the thread that sets the appropriate flag never got the chance to run.
Conceptually, another way to think about this is we have multiple pieces of code that are designed such that multiple of them need to start simultaneously, but locks are contrary to that notion
Replacing synchronized with atomic will cause both threads locking in the for loop. The memory operation result would be visible to other threads only after leaving the atomic block. Although thread 1 and thread 2 modify flagA and flagB to true in their blocks, they still cannot not see the updated value because the other thread has not left the atomic block.
This is like forcing two sections of inter-dependent code to be mutually exclusive.
I think there will be a RW conflict detected between the two transactions and so they will be serialized. But since the two transactions are dependent on each other for their completion there will be a deadlock.
Synchronize() creates a lock to a piece of code. Other concurrent thread will see the result as soon as it executes. But the TM will not commit writing operation until the whole atomic part has been completed.