Mutual Exclusion Problem

后端 未结 6 1104
陌清茗
陌清茗 2021-01-13 16:47

Please take a look on the following pseudo-code:

boolean blocked[2];
int turn;
void P(int id) {
      while(true) {
             blocked[id] = true;
                 


        
6条回答
  •  星月不相逢
    2021-01-13 17:30

    Concurrency can not be implemented like this, especially in a multi-processor (or multi-core) environment: different cores/processors have different caches. Those caches may not be coherent. The pseudo-code below could execute in the order shown, with the results shown:

    get blocked[0] -> false   // cpu 0
    set blocked[0] = true     // cpu 1 (stored in CPU 1's L1 cache)
    get blocked[0] -> false   // cpu 0 (retrieved from CPU 0's L1 cache)
    get glocked[0] -> false   // cpu 2 (retrieved from main memory)
    

    You need hardware knowledge to implement concurrency.

提交回复
热议问题