- I have searched open and closed issues and pull requests for duplicates, using these search terms:
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch21-02-multithreaded.html
Description of the problem:
The code in Listing 21-20 that uses let job = receiver.lock().unwrap().recv().unwrap(); works because with let, any temporary values used in the expression on the right-hand side of the equal sign are immediately dropped when the let statement ends. However, while let (and if let and match) does not drop temporary values until the end of the associated block. In Listing 21-21, the lock remains held for the duration of the call to job(), meaning other Worker instances cannot receive jobs.
Suggested fix:
Not because of holding the lock during job(), but because of holding the lock during waiting for receipt (recv() blocking).
Rust's temporary value rule ensures that in a while let, temporary values generated by expressions (such as MutexGuard) do not persist until the end of the code block, but are immediately discarded after pattern matching is complete.
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch21-02-multithreaded.html
Description of the problem:
The code in Listing 21-20 that uses let job = receiver.lock().unwrap().recv().unwrap(); works because with let, any temporary values used in the expression on the right-hand side of the equal sign are immediately dropped when the let statement ends. However, while let (and if let and match) does not drop temporary values until the end of the associated block. In Listing 21-21, the lock remains held for the duration of the call to job(), meaning other Worker instances cannot receive jobs.
Suggested fix:
Not because of holding the lock during job(), but because of holding the lock during waiting for receipt (recv() blocking).
Rust's temporary value rule ensures that in a while let, temporary values generated by expressions (such as MutexGuard) do not persist until the end of the code block, but are immediately discarded after pattern matching is complete.