educative.io

Interlocked required for increment/decrement operations?

I thought the -- and ++ operations are not atomic, i.e. two threads doing ++ could both read 2 and write back 3 - but the correct value should be 4.


Type your question above this line.

Course: https://www.educative.io/collection/5307417243942912/6345276711239680
Lesson: https://www.educative.io/collection/page/5307417243942912/6345276711239680/6650923076026368

Okay, I see. Because the arrived() method is protected by a Monitor, only one thread can be active inside the method at any given time. The other threads will be blocked on one of the Monitor.Wait(padlock). Thus, the increment/decrement operations are protected.