January 2019
Intermediate to advanced
520 pages
14h 32m
English
In the first optimization, we will replace Mutex with RwLock, because Mutex is locked for both reading and writing, but RwLock allows us to have a single writer or multiple readers. It allows you to avoid blocking for reading the value if no one updates the value. This applies to our example, as we rarely update a shared value, but have to read it from multiple instances of handlers.
RwLock is an alternative to Mutex that separates readers and writers, but the usage of RwLock is as simple as Mutex. Replace Mutex to RwLock in the State struct:
#[derive(Clone)]struct State { // last_minute: Arc<Mutex<String>>, last_minute: Arc<RwLock<String>>,}
Also, we have to replace a creation of the last_minute reference counter ...
Read now
Unlock full access