Rust programming video tutorial (Advanced) — scalable concurrency of 019 sync and send trait

Time:2021-9-13

Video address

Headline address:https://www.ixigua.com/i677586170644791348…
Station B address:https://www.bilibili.com/video/av81202308/

Source address

GitHub address:https://github.com/anonymousGiga/learn_rus…

Explanation content

1. There are two concurrency concepts embedded in the language: sync in STD:: marker and send trait.

2. Allow transfer of ownership between threads through send
(1) The send flag trait indicates that ownership of the type can be passed between threads. Almost all rust types are send, but exceptions: for example, RC < T > cannot send.
(2) Any type that consists entirely of the send type is also automatically marked send.

3. Sync allows multi-threaded access
(1) The sync flag trait indicates that a type that implements sync can safely have a reference to its value in multiple threads, that is, for any type T, if & T (the reference of T) is send, t is sync, which means that its reference can be safely sent to another thread.
(2) The smart pointer RC < T > is not sync either, for the same reason that it is not send. Refcell < T > and cell < T > series types are not synchronous. The borrowing check performed by refcell < T > at runtime is not thread safe, and mutex < T > is synchronous.

4. It is not safe to implement send and sync manually
Generally, it is not necessary to manually implement send and sync traits, because the types composed of send and sync types are automatically sent and sync. Because they are tag traits, they don’t even need to implement any methods. They are only used to enhance concurrency related immutability.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Linghu rushed