Concurrency

The ownership rules pay for themselves here: a data race is a compile error before it is a crash. These fourteen lessons spawn a thread and join it, pass messages through a channel, and share a counter through Mutex and Arc. A condition variable, the atomics and the memory orderings follow, then Send and Sync and the compile error that explains why Rc cannot cross a thread. Scoped threads and a thread pool come next, the producer and consumer queue, the async function and the Future an executor polls, the iterator that keeps its state, and the split of a heavy job across threads.

089 Threads and join handles thread::spawn, JoinHandle, move closures and two threads that print (C++ 105) Open 090 Channels between threads mpsc::channel, send and recv, a worker that reports back and a channel that closes (C++ 111) Open 091 Mutex, Arc and shared state Mutex, lock and the guard, Arc for shared ownership and a counter that stops losing updates (C++ 106) Open 092 Condition variables Condvar, wait and notify_one, a predicate and a producer that hands work to a consumer (C++ 107) Open 093 Atomics and fetch_add AtomicUsize, fetch_add, compare_exchange and the counter without a mutex (C++ 108) Open 094 The memory model and orderings happens-before, Relaxed, Acquire and Release, and a flag that publishes data (C++ 109) Open 095 Send, Sync and thread safety the two marker traits, why Rc is not Send, a type that is Send but not Sync and a compile error that explains it (no C++ counterpart) Open 096 Scoped threads thread::scope, a borrow shared with a thread and no Arc where a scope is enough (C++ 114) Open 097 A thread pool a queue of jobs, worker threads over Arc<Mutex<Receiver>>, and a pool that shuts down (C++ 112) Open 098 Producer, consumer and queues a bounded queue, back pressure, RwLock readers and a worker that stops on a flag (C++ 113) Open 099 async and await an async fn, a Future, await inside async and why the standard library has no executor (C++ 115) Open 100 Futures and a small executor the Future trait, poll and Context, a block_on written by hand and tokio named in words (C++ 115, 116) Open 101 Iterators that keep state a state machine as an iterator, an iterator that yields from a loop, and a lazy sequence (C++ 116) Open 102 Splitting work across threads chunks, a thread per chunk, a sum collected back and the standard library against a parallel iterator crate (C++ 117) Open

Lessons