Concurrency 为什么';如果做得不正确,餐厅是否会陷入僵局?
根据,他们的基于互斥体的哲学家进餐问题实现避免了死锁,方法是始终选择ID最低的fork作为每个哲学家的左fork,即,使一个左撇子:Concurrency 为什么';如果做得不正确,餐厅是否会陷入僵局?,concurrency,rust,mutex,Concurrency,Rust,Mutex,根据,他们的基于互斥体的哲学家进餐问题实现避免了死锁,方法是始终选择ID最低的fork作为每个哲学家的左fork,即,使一个左撇子: let philosophers = vec![ Philosopher::new("Judith Butler", 0, 1), Philosopher::new("Gilles Deleuze", 1, 2), Philosopher::new("Karl Marx", 2, 3), Philos
let philosophers = vec![
Philosopher::new("Judith Butler", 0, 1),
Philosopher::new("Gilles Deleuze", 1, 2),
Philosopher::new("Karl Marx", 2, 3),
Philosopher::new("Emma Goldman", 3, 4),
Philosopher::new("Michel Foucault", 0, 4),
];
但是,如果我违反此规则并在最后一个中交换fork索引,那么程序仍然可以运行,不会出现死锁或恐慌
我尝试过的其他事情:
- 延长
eat()函数调用中的sleep参数
- 对睡眠争论的评论
- 将主体包装在
循环{}
中,以查看它最终是否会发生
我要怎么做才能正确地打破它?
以下是完整的源代码,没有任何上述更改:
use std::thread;
use std::sync::{Mutex, Arc};
struct Philosopher {
name: String,
left: usize,
right: usize,
}
impl Philosopher {
fn new(name: &str, left: usize, right: usize) -> Philosopher {
Philosopher {
name: name.to_string(),
left: left,
right: right,
}
}
fn eat(&self, table: &Table) {
let _left = table.forks[self.left].lock().unwrap();
let _right = table.forks[self.right].lock().unwrap();
println!("{} is eating.", self.name);
thread::sleep_ms(1000);
println!("{} is done eating.", self.name);
}
}
struct Table {
forks: Vec<Mutex<()>>,
}
fn main() {
let table = Arc::new(Table { forks: vec![
Mutex::new(()),
Mutex::new(()),
Mutex::new(()),
Mutex::new(()),
Mutex::new(()),
]});
let philosophers = vec![
Philosopher::new("Judith Butler", 0, 1),
Philosopher::new("Gilles Deleuze", 1, 2),
Philosopher::new("Karl Marx", 2, 3),
Philosopher::new("Emma Goldman", 3, 4),
Philosopher::new("Michel Foucault", 0, 4),
];
let handles: Vec<_> = philosophers.into_iter().map(|p| {
let table = table.clone();
thread::spawn(move || {
p.eat(&table);
})
}).collect();
for h in handles {
h.join().unwrap();
}
}
使用std::thread;
使用std::sync::{Mutex,Arc};
结构哲学家{
名称:String,
左:usize,
右:usize,
}
朴素的哲学家{
fn new(名称:&str,左:usize,右:usize)->{
哲人{
name:name.to_string(),
左:左,,
对:对,
}
}
fn eat(&self,表:&table){
让_left=table.forks[self.left].lock().unwrap();
让_right=table.forks[self.right].lock().unwrap();
println!(“{}正在吃东西。”,self.name);
线程:sleep_ms(1000);
println!(“{}吃完了。”,self.name);
}
}
结构表{
福克斯:Vec,
}
fn main(){
让table=Arc::new(table{forks:vec[
互斥体::新(()),
互斥体::新(()),
互斥体::新(()),
互斥体::新(()),
互斥体::新(()),
]});
让哲学家=vec[
哲学家:新(“朱迪思·巴特勒”,0,1),
哲学家:新(“吉勒斯·德勒兹”,1,2),
哲学家:新的(“卡尔·马克思”,2,3),
哲学家:新的(“艾玛·戈德曼”,3,4),
哲学家:新(“米歇尔·福柯”,0,4),
];
让handles:Vec=phiges.into_iter().map(| p|{
让table=table.clone();
线程::生成(移动| |{
p、 吃(和桌子);
})
}).收集();
用于手柄中的h{
h、 join().unwrap();
}
}
PS:遗憾的是,当前的Rust文档没有包括这个例子,因此上面的链接被破坏。当每个哲学家“同时”拿起他/她的左边的叉子,然后发现他/她的右边的叉子已经被拿走时,僵局就出现了。为了让这种情况经常发生,你需要在“同时性”中引入一些模糊的因素,这样,如果哲学家们都在彼此之间的一定时间内拿起他们的左叉子,那么他们中没有一个人能够拿起他们的右叉子。换句话说,您需要在拿起两个叉子之间引入一点睡眠:
fn eat(&self, table: &Table) {
let _left = table.forks[self.left].lock().unwrap();
thread::sleep_ms(1000); // <---- simultaneity fudge factor
let _right = table.forks[self.right].lock().unwrap();
println!("{} is eating.", self.name);
thread::sleep_ms(1000);
println!("{} is done eating.", self.name);
}
fn eat(&self,table:&table){
让_left=table.forks[self.left].lock().unwrap();
thread::sleep_ms(1000);//太棒了。这会立即死锁,并且反转索引绝对可以防止死锁的发生。非常棒而且简单。这应该复制到文档中