We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
let v = vec![1, 2, 3]; let handle = thread::spawn(move || { println!("Here's a vector: {:?}", v); }); handle.join().unwrap();
vector v
handle.join().unwrap()
Rc<i32>
Send
T
&mut T
mut
drop
move
RefCell<usize>
Mutex<RefCell<usize
sd::sync::Arc
std::sync::Mutex
std::sync::RwLock
Sync 能理解为是 Send 的辅助
一个类型实现了Sync trait,不一定就实现了Send trait,
如果一个类型实现了Send trait,那么它一定也实现了Sync trait。
Sync:
满足Sync约束的类型,能在多线程之间安全的共享使用(Shared access is thread-safe)。
Sync
满足Sync约束的类型T,只表示该类型能在多个线程中读共享,即:不能move,也不能修改,仅仅只能通过引用&T来读取这个值。
&T
一个类型&T 的只有在满足Send约束的条件下,类型T 才能满足Sync约束 (a type T is Sync if and only if &T is Send)。即:T: Sync ≡ &T: Send。
T: Sync ≡ &T: Send
这是因为将数据的引用发送到另一个线程并不会实际移动数据,而只是共享对数据的指针。
因此,如果安全地将数据的引用发送到另一个线程,则该数据本身也能同时从多个线程访问。
对于那些基本的类型(primitive types)而言,比如i32类型,大多是同时满足Send和Sync这两个约束的,因为这些类型的共享引用(&)既能在多个多个线程中使用。
i32
#type/rust #public
The text was updated successfully, but these errors were encountered:
No branches or pull requests
参考资料
如何保证共享数据多线程安全
共享只读数据
(concept:: Sync 和 Send, 'static)
Send
vector v
在主线程创建以后,直接 move 给了生成的线程,那么除了那个线程,没有其他的地方能使用这个 vector。handle.join().unwrap()
)前面尝试打印 vector,Rust 就会报错Rc<i32>
不能 send 在 threads safelySend 的要求
Send
约束的类型,能在多线程之间安全的排它使用,所有权类型Send
约束的类型T
,表示T
和&mut T
(mut
表示能修改这个引用,甚至于删除即drop
这个数据)这两种类型的数据能在多个线程之间传递move
值以及修改引用到的值。Sync
RefCell<usize>
改成Mutex<RefCell<usize
>>sd::sync::Arc
、std::sync::Mutex
、std::sync::RwLock
等Sync 和 Send 的关系
Sync 能理解为是 Send 的辅助
一个类型实现了Sync trait,不一定就实现了Send trait,
如果一个类型实现了Send trait,那么它一定也实现了Sync trait。
Sync:
满足
Sync
约束的类型,能在多线程之间安全的共享使用(Shared access is thread-safe)。满足
Sync
约束的类型T
,只表示该类型能在多个线程中读共享,即:不能move
,也不能修改,仅仅只能通过引用&T
来读取这个值。一个类型&T 的只有在满足
Send
约束的条件下,类型T 才能满足Sync
约束 (a type T is Sync if and only if &T is Send)。即:T: Sync ≡ &T: Send
。这是因为将数据的引用发送到另一个线程并不会实际移动数据,而只是共享对数据的指针。
因此,如果安全地将数据的引用发送到另一个线程,则该数据本身也能同时从多个线程访问。
对于那些基本的类型(primitive types)而言,比如
i32
类型,大多是同时满足Send
和Sync
这两个约束的,因为这些类型的共享引用(&)既能在多个多个线程中使用。(concept:: 'static )
什么是'static 的生命周期呢?
#type/rust #public
The text was updated successfully, but these errors were encountered: