Threads synchronization. How exactly lock makes access to memory 'correct'?

后端 未结 5 1178
清歌不尽
清歌不尽 2020-12-29 09:41

First of all, I know that lock{} is synthetic sugar for Monitor class. (oh, syntactic sugar)

I was playing with simple mul

5条回答
  •  执念已碎
    2020-12-29 10:21

    We have been discussing this with deafsheep and our current idea can be represented as the following schema

    enter image description here

    Time is running left to right, and 2 threads are represented by two rows.

    where

    • black box represents process of acquiring, holding and releasing the lock
    • plus represents addition operation ( schema represents scale on my PC, lock takes approximated 20 times longer than add)
    • white box represents period that consists of try to acquire lock, and further awaiting for it to become available

    Order of black boxes is always like this, they cannot overlap , and they should always follow each other very closely. Consequently, it becomes very logical, that pluses never overlap, and we should come up precisely to expected sum.

    The source of existing error is explored in this question:

提交回复
热议问题