Description
Currently, the only Rust guide that talks about concurrency is the task guide, which talks about spawn
, channels, Futures and Arcs and how to use them.
However, Rusts concurrency story is not based on those library types, its based on its ownership and borrowing semantic and the two build-in traits Send
and Sync
.
A rewritten concurrency guide could start by talking around the two core problems with concurrent/parallel code:
- Making sure that objects created on one thread can be safely moved into another thread and then used there. (Examples: global allocator vs thread-local allocator, FFI bindings that need to stay on one thread)
- Making sure that objects can be safely accessed from different threads in parallel. (Examples: worker threads with shared memory, global variables)
And then talk about how those two cases are, per definition, covered with the two build-in traits:
Send
expresses whether a type can be safely ownership-transferred across thread boundariesSync
expresses whether a type can be safely accessed through shared references from different threads.
After each of these those two traits got explained, the guide could go into details about language semantics and std library APIs that make use of them, eg task spawning works with Send
, Arc
s are Send
and require Sync
on the inner type, while channels are sendable endpoints that can only transfer sendable types, static
variables that contain Sync
types are safe to access, etc.