Multithreading 此算法在确保临界截面不为';你没打断我吗?

Multithreading 此算法在确保临界截面不为';你没打断我吗?,multithreading,algorithm,language-agnostic,deadlock,race-condition,Multithreading,Algorithm,Language Agnostic,Deadlock,Race Condition,从 全局变量turn用于指示要进入的下一个进程 关键部分。转弯的初始值可以是0或1 int-turn=1 T0: while (true) { while (turn != 0) { ; } (1) critical section (2) turn = 1; (3) non-critical section (4) } while (true) { while (turn != 1) { ; } (1) critical section (2) turn = 0;

全局变量turn用于指示要进入的下一个进程 关键部分。转弯的初始值可以是0或1

int-turn=1

T0:

while (true) {
  while (turn != 0) { ; } (1)
  critical section (2)
  turn = 1; (3)
  non-critical section (4)
}
while (true) {
  while (turn != 1) { ; } (1)
  critical section (2)
  turn = 0; (3)
  non-critical section (4)
}
T1:

while (true) {
  while (turn != 0) { ; } (1)
  critical section (2)
  turn = 1; (3)
  non-critical section (4)
}
while (true) {
  while (turn != 1) { ; } (1)
  critical section (2)
  turn = 0; (3)
  non-critical section (4)
}

我不明白问题出在哪里。为什么T0会在(转动!=1)
的同时重复
?如果上下文切换到T1,则它将进入其关键部分,然后设置
turn=0


编辑:我现在明白为什么T0会永远等待了。违反的“规则”有名字吗?例如,在线程的上下文中有“互斥”、“进度”、“有界等待”、“没有关于线程/进程相对速度的假设”,那么这些假设中是否有一个未得到满足?

您缺少问题描述中的第二个假设:
螺纹可以在非临界段终止。您在此处复制的描述指定“T1终止于非关键部分”,因此T1将不再设置turn=0。

您缺少问题描述中的第二个假设:
螺纹可以在非临界段终止。您在此处复制的描述指定了“T1终止于非关键部分”,因此T1将不再设置turn=0。

根据T1(4)处的注释,T1不再存在。它因其他未指明的原因而终止。所以它永远不能将turn设置回0。根据T1(4)的评论,T1已经不存在了。它因其他未指明的原因而终止。所以它永远不能将turn设置回0。因此,T0将永远被卡住。