异步知识点汇总
对于任意类型 T,若是 &T是 Send ,T 就是 Sync 的
unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {} unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {} unsafe impl<T: ?Sized> Send for RefCell<T> where T: Send {} impl<T: ?Sized> !Sync for RefCell<T> {} unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
&mut self 并不要求 T: Sync
mod impls { #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<T: Sync + ?Sized> Send for &T {} #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<T: Send + ?Sized> Send for &mut T {} }
&T: Send 要求 T: Sync,而 &mut T 则 T: Send 便可
要共享数据时使用 Arc<T>
要求 T: Send + Sync。而共享可写数据,使用 Arc<Mutex<T>>
,要求 T: Send 便可,不要求Sync。
对于原子类的操作,如果是写,那么内存顺序提供 Release,如果是读,那么内存顺序提供 Acquire。