460392 (1) [Avatar] Offline
#1
I know that ReentrantLock use AbstractQueuedSynchronizer(AQS) to implement Lock.
But the detail of the realization I can not understand.

I know that AQS use volatile, CAS and spin for synchronizing. But, these actions only control the "state" member.
Although, LockSupport.park and LockSupport.unpark can synchronize the cacheline of the thread.
But if there is never contention, LockSupport methods will never be called.
Like this:
1.Thread A start and run
2.Thread B start and run
3.Thread A:
lock.lock();
try{
//modify some shared members
}finally{
lock.unlock();
}
4.Then Thread B:
lock.lock();
try{
//read shared members
}finally{
lock.unlock();
}
No contention, thread B does not call LockSupport methods.

lock.lock() and lock.unlock() only CAS "state" member.

Why thread B can seen the modification of thread A about shared members?


Thanks!