There's more...

Despite its nice usability, RwLock is still no silver bullet for all concurrent problems. There is a concept in concurrent programming called deadlock. It arises when two processes wait for the unlocking of resources that the other holds. This will lead to them waiting forever, as no one is ready to take the first step. Kind of like teenagers in love. An example of this would be a writer_a requesting access to a file that writer_b holds. writer_b, in the meantime, needs some kind of user information from writer_a before he can give up the file lock. The best way to avoid this problem is to keep it in the back of your mind and remember it when you're about to create processes that depend on each other.

Another lock that is fairly popular in other languages is the Mutex, which Rust also provides under std::sync::Mutex. When it locks resources, it treats every process like a writer, so no two threads will ever be able to work at the same time with a Mutex, even if they don't mutate the data. We are going to create a very simple implementation of it ourselves in the next recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.82.253