11## Graceful Shutdown and Cleanup
22
3- The code in Listing 20-21 is responding to requests asynchronously through the
3+ The code in Listing 20-20 is responding to requests asynchronously through the
44use of a thread pool, as we intended. We get some warnings about the ` workers ` ,
55` id ` , and ` thread ` fields that we’re not using in a direct way that reminds us
66we’re not cleaning up anything. When we use the less elegant <span
@@ -18,7 +18,7 @@ accept only two requests before gracefully shutting down its thread pool.
1818
1919Let’s start with implementing ` Drop ` on our thread pool. When the pool is
2020dropped, our threads should all join to make sure they finish their work.
21- Listing 20-23 shows a first attempt at a ` Drop ` implementation; this code won’t
21+ Listing 20-22 shows a first attempt at a ` Drop ` implementation; this code won’t
2222quite work yet.
2323
2424<span class =" filename " >Filename: src/lib.rs</span >
@@ -35,7 +35,7 @@ impl Drop for ThreadPool {
3535}
3636```
3737
38- <span class =" caption " >Listing 20-23 : Joining each thread when the thread pool
38+ <span class =" caption " >Listing 20-22 : Joining each thread when the thread pool
3939goes out of scope</span >
4040
4141First, we loop through each of the thread pool ` workers ` . We use ` &mut ` for
@@ -178,7 +178,7 @@ thread should run, or it will be a `Terminate` variant that will cause the
178178thread to exit its loop and stop.
179179
180180We need to adjust the channel to use values of type ` Message ` rather than type
181- ` Job ` , as shown in Listing 20-24 .
181+ ` Job ` , as shown in Listing 20-23 .
182182
183183<span class =" filename " >Filename: src/lib.rs</span >
184184
@@ -236,7 +236,7 @@ impl Worker {
236236}
237237```
238238
239- <span class =" caption " >Listing 20-24 : Sending and receiving ` Message ` values and
239+ <span class =" caption " >Listing 20-23 : Sending and receiving ` Message ` values and
240240exiting the loop if a ` Worker ` receives ` Message::Terminate ` </span >
241241
242242To incorporate the ` Message ` enum, we need to change ` Job ` to ` Message ` in two
@@ -248,9 +248,9 @@ received, and the thread will break out of the loop if the `Terminate` variant
248248is received.
249249
250250With these changes, the code will compile and continue to function in the same
251- way as it did after Listing 20-21 . But we’ll get a warning because we aren’t
251+ way as it did after Listing 20-20 . But we’ll get a warning because we aren’t
252252creating any messages of the ` Terminate ` variety. Let’s fix this warning by
253- changing our ` Drop ` implementation to look like Listing 20-25 .
253+ changing our ` Drop ` implementation to look like Listing 20-24 .
254254
255255<span class =" filename " >Filename: src/lib.rs</span >
256256
@@ -276,7 +276,7 @@ impl Drop for ThreadPool {
276276}
277277```
278278
279- <span class =" caption " >Listing 20-25 : Sending ` Message::Terminate ` to the
279+ <span class =" caption " >Listing 20-24 : Sending ` Message::Terminate ` to the
280280workers before calling ` join ` on each worker thread</span >
281281
282282We’re now iterating over the workers twice: once to send one ` Terminate `
@@ -302,7 +302,7 @@ messages as there are workers, each worker will receive a terminate message
302302before ` join ` is called on its thread.
303303
304304To see this code in action, let’s modify ` main ` to accept only two requests
305- before gracefully shutting down the server, as shown in Listing 20-26 .
305+ before gracefully shutting down the server, as shown in Listing 20-25 .
306306
307307<span class =" filename " >Filename: src/bin/main.rs</span >
308308
@@ -323,7 +323,7 @@ fn main() {
323323}
324324```
325325
326- <span class =" caption " >Listing 20-26 : Shut down the server after serving two
326+ <span class =" caption " >Listing 20-25 : Shut down the server after serving two
327327requests by exiting the loop</span >
328328
329329You wouldn’t want a real-world web server to shut down after serving only two
0 commit comments