What happens if two process in different processors try to acquire the lock at EXACTLY same time

前端 未结 5 1233
忘掉有多难
忘掉有多难 2020-12-13 02:48

Ok, so I am reading about synchronization, and I read through various algorithms such as spinlocks, semaphores, and mutex to avoid race condition.

However, these alg

5条回答
  •  一向
    一向 (楼主)
    2020-12-13 03:21

    You might want to look into memory barriers.

    http://en.wikipedia.org/wiki/Memory_barrier

    In this case the locks would use memory barriers so that the internal value used in the lock cannot be accessed by multiple processors at once.

    Some architectures also allow locking of all cores except 1 to allow for this. For example x86 sports a LOCK prefix that when added to instructions will lock access to memory during that instruction. (e.g: LOCK ADD EAX, 1 for an atomic increment to a register)

    Architectures that don't support LOCK or atomic instructions use compare and exchange or test and set/swap. Usually it involves a small instruction loop that in high level may look like

    while (target != value) target = value;
    

    This may not look like it will execute more than once but it ensures that in between the instructions the value is not change from underneath it. The downside to this approach is if there is high contention on target then it may eat up more clock cycles than you would like but it tends to happen fast enough so it's never really noticeable.

提交回复
热议问题