开发者

Lamport's mutual exclusion

开发者 https://www.devze.com 2023-01-25 03:07 出处:网络
given the lamport\'s fast mutex algorithm, i\'m trying to understand why are the awaits there. The algorithm:

given the lamport's fast mutex algorithm, i'm trying to understand why are the awaits there.

The algorithm:

start: b[i] = true
      x = i 
      if y != 0 then b[i] = false
                          await y = 0
                          goto start 
      fi
      y = i
      if x != i then b[i] = false
                         for j = 1 to n do await b[j] = false od
                         if y 开发者_JAVA百科!= i then await y = 0
                                            goto start 
                         fi 
      fi
      critical section
      y = 0
      b[i] = false 

Can't we just remove both of them?


You want to wait (block) instead of spin (continually execute checking for availability) while the other process/thread is in the critical section, otherwise you're taking up resources that could be used elsewhere. If only one thread can run at a time, then you would prevent the thread in the critical section from running during your entire timeslice resulting in the program taking much longer to complete.

0

精彩评论

暂无评论...
验证码 换一张
取 消