Send and Sync Traits
-
Sync
allows an object to be used by multiple threads at the same time.&T
being sendable to multipel threads allows this naturally.- How to simulate
Sync
for an arbitrary type?Mutex
orRwLock
allows only one thread to access the data. This behavior enforcing the sharing order of the data can make non-Sync
object work like aSync
object.- Use atomic types
- Non-
Sync
: can be only used by a single thread at any single time (their reference cannot be moved or copied to other thread)
-
Send
allows an object to be used by multiple threads at different times.- Rust's ownership model and move semantics allow the non-overlapping use of the object.
- Non-
Send
: can only ever be onwed by a single thread (self cannot be moved or copied to other thread)
-
<Sync + !Send>
: If the object can be used safely by two threads at the same time