时间:2019年3月21日,周四晚上10点
内容:Rholang如何优雅地解决dining philosophers问题
时长:1小时
主持:愁虫
会议链接: zoom.us/j/353688314
1. 用petri nets图描述一种正确的解决方法:(动图)
2 用python的线程锁的实现方法:
from threading import Thread, Lock
# Set the table
fork = Lock()
knife = Lock()
# Philosopher 1's plan
def plan1():
while True:
if fork.acquire(False):
if knife.acquire(False):
print("Philosopher 1 is full.")
knife.release()
fork.release()
phil1 = Thread(target=plan1)
# Likewise for philosopher 2
def plan2():
while True:
if knife.acquire(False):
if fork.acquire(False):
print("Philosopher 2 is full.")
fork.release()
knife.release()
phil2 = Thread(target=plan2)
# Hard to give them a fair start in a sequential language
phil1.start()
phil2.start()
3 用Rholang优雅地实现:
new log(`rho:io:stdout`), north, south, knife, spoon in {
// Set the table
north!(*knife) |
south!(*spoon) |
// Philosopher 1's plan
for (@knf <- north; @spn <- south) {
log!("Philosopher 1 is full.") |
north!(knf) |
south!(spn)
} |
// Likewise for philosopher 2
for (@spn <- south; @knf <- north) {
log!("Philosopher 2 is full.") |
north!(knf) |
south!(spn)
}
}
注:代码来自Joshy的例子:blog.rchain.coop/blog/2018/09/15/rholang-vs-the-dining-philosophers/