Closed
Description
Right now, MutexGuard<Cell<i32>>
satisfies the Sync
bound. That is rather bad, because it lets me write a program that has a data race:
use std::sync::Mutex;
use std::cell::Cell;
extern crate rayon;
fn main()
{
let m = Mutex::new(Cell::new(0));
let g = m.lock().unwrap();
{
rayon::join(
|| { g.set(g.get() + 1); println!("Thread 1: {:?}", g.get()) },
|| { g.set(g.get() + 1); println!("Thread 2: {:?}", g.get()) });
}
}
The get
and set
calls in the two threads are unsynchronized (as usual for a Cell
), and they are racing. This is a soundness bug.
The cause for this is that MutexGuard<T>
implements Sync
whenever T
implements Send
, which is plain wrong. The fix is to let MutexGuard<T>
implement Sync
whenever T
implements Sync
. I will submit a PR soon.