Python 3.x Python中使用五个锁的问题
我不明白这部分,Python 3.x Python中使用五个锁的问题,python-3.x,concurrency,dining-philosopher,Python 3.x,Concurrency,Dining Philosopher,我不明白这部分, 使用self.locks[第一个]: 使用self.locks[秒]: 双嵌套的如何确定谁先选择左边的,谁先选择右边的 有人能解释一下订单是如何运作的吗 from threading import Lock class DiningPhilosophers: def __init__(self): self.locks = [Lock() for _ in range(5)] # call the functions directly
使用self.locks[第一个]:
使用self.locks[秒]:
双嵌套的如何确定谁先选择左边的,谁先选择右边的 有人能解释一下订单是如何运作的吗
from threading import Lock
class DiningPhilosophers:
def __init__(self):
self.locks = [Lock() for _ in range(5)]
# call the functions directly to execute, for example, eat()
def wantsToEat(self, philosopher: int, pickLeftFork: 'Callable[[], None]',
pickRightFork: 'Callable[[], None]',
eat: 'Callable[[], None]',
putLeftFork: 'Callable[[], None]',
putRightFork: 'Callable[[], None]') -> None:
if philosopher != 0:
first, second = philosopher, (philosopher + 1) % 5
else:
second, first = philosopher, (philosopher + 1) % 5
with self.locks[first]:
with self.locks[second]:
pickLeftFork()
pickRightFork()
eat()
putLeftFork()
putRightFork()