Estimated time: 1 day
Rust has Send
and Sync
marker traits which are fundamental for concurrency and thread safety story in Rust and represent one of fearless concurrency corner stones (which allow to avoid data races at compile time).
For better understanding Send
/Sync
purpose, design, limitations and use cases, read through the following articles:
- Official
Send
docs - Official
Sync
docs - Rust Book: 16.4. Extensible Concurrency with the Sync and Send Traits
- Rustonomicon: 8.2. Send and Sync
- Huon Wilson: Some notes on Send and Sync
- Piotr Sarnacki: Arc and Mutex in Rust
- nyanpasu64: An unsafe tour of Rust's Send and Sync
- Josh Haberman: Thread Safety in C++ and Rust
- Cliff L. Biffle: Safely writing code that isn't thread-safe
- Louis Dureuil: Too dangerous for C++
Implement the following types, which meet conditions:
OnlySync
isSync
, but!Send
.OnlySend
isSend
, but!Sync
.SyncAndSend
is bothSync
andSend
.NotSyncNotSend
is both!Sync
and!Send
.
All inner details of implementation are on your choice.
Play with these types from multiple threads to see how compile time fearless concurrency works in practice.
After completing everything above, you should be able to answer (and understand why) the following questions: