スケジューリングの優先順位に偏りがあると、スレッドの実行が平等にならない。
import time
from threading import Thread
from lock_with_name import LockWithName
dumplings = 120
class Philosopher(Thread):
def __init__(self, name: str,
left_chopstick: LockWithName,
right_chopstick: LockWithName):
super().__init__()
self.name = name
self.left_chopstick = left_chopstick
self.right_chopstick = right_chopstick
def run(self) -> None:
global dumplings
dumplings_eaten = 0
while dumplings > 0:
self.left_chopstick.acquire()
self.right_chopstick.acquire()
if dumplings > 0:
dumplings -= 1
dumplings_eaten += 1
time.sleep(1e-16)
self.right_chopstick.release()
self.left_chopstick.release()
print(f"{self.name} took {dumplings_eaten} pieces")
if __name__ == "__main__":
chopstick_a = LockWithName("chopstick_a")
chopstick_b = LockWithName("chopstick_b")
threads = []
for i in range(10):
threads.append(
Philosopher(f"Philosopher #{i}", chopstick_a, chopstick_b))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
$ python3 starvation.py
Philosopher #4 took 51 pieces
Philosopher #9 took 0 pieces
Philosopher #1 took 0 pieces
Philosopher #3 took 0 pieces
Philosopher #7 took 0 pieces
Philosopher #8 took 0 pieces
Philosopher #6 took 0 pieces
Philosopher #2 took 0 pieces
Philosopher #5 took 0 pieces
Philosopher #0 took 69 pieces