cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
593 stars 92 forks source link

Use of MutexGuard in match expressiond prevents threads from running in parallel #60

Open aryzing opened 1 year ago

aryzing commented 1 year ago

URL to the section(s) of the book with this problem:

https://github.com/cognitive-engineering-lab/rust-book/blob/ac7e98be8fdbd0ee81f2eeb049754a2b403631e8/listings/ch20-web-server/listing-20-24/src/lib.rs#L73

Description of the problem:

As the book warns just in the section prior to the one linked above, using a MutexGuard in a match expression prevents mutex from being unlocked and the threads from running in parallel.

Suggested fix:

+            let message = receiver.lock().unwrap().recv();
+
-            match receiver.lock().unwrap().recv() { 
+            match message {