distributed-systemlocks

distributed systems, lamport and vector clock and locking


Was going through the some implementations of Lamport and vector clocks. The one thing that struck me and confused me was they all just incremented time's value without ever locking it.

My thinking was, it should have been:

acquireLock()
time += 1
releaseLock()

Am I missing something? Would it not lead to incorrect time values for events generated concurrently? Or does this impact does not impact the system as whole (it should though)?

Thanks!


Solution

  • Answer after the comment:

    My question was in their implementation, we need to increase the counter every time a new event happens. To increase this counter (which is actually the lamport timestamp) do we do it in a syncronized way or just increment it?

    In most languages "time += 1" is not an atomic operation. So if there are two threads executing the same increment at the same time, there is a non zero chance that one of these updates will be lost. To avoid this problem, the code uses local lock.

    In some platform, like java, there are thread friendly structures which support increments as atomic operations, so explicit locking is not needed.

    And many systems actually use single thread for processing events; clearly no need in locking in that case either. It seems in this particular example this is the case.