Shared state model
Using shared state to communicate values to a thread is the most widely used approach, and the synchronization primitives to achieve this exist in most mainstream languages. Synchronization primitives are types or language constructs that allow multiple threads to access or manipulate a value in a thread-safe way. Rust also has many synchronization primitives that we can wrap around types to make them thread-safe.
As we saw in the previous section, we cannot have shared access to any value from multiple threads. We need shared ownership here. Back in Chapter 5, Memory Management and Safety, we introduced the Rc type. that can provide shared ownership of values. Let's try using this type with our previous example of reading ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access