You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rust.tex
+82-1
Original file line number
Diff line number
Diff line change
@@ -3233,9 +3233,90 @@ \section{Multithread Web Server}
3233
3233
3234
3234
\begin{frame}[fragile]
3235
3235
\frametitle{Improving Throughput with a Thread Pool}
3236
-
\inputminted{rust}{./code/http2.rs}
3236
+
A thread pool is a group of spawned threads that are waiting and ready to handle a task. When the program receives a new task, it assigns one of the threads in the pool to the task, and that thread will process the task. The remaining threads in the pool are available to handle any other tasks that come in while the first thread is processing. When the first thread is done processing its task, it’s returned to the pool of idle threads, ready to handle a new task. A thread pool allows you to process connections concurrently, increasing the throughput of your server.
3237
+
\end{frame}
3238
+
3239
+
3240
+
\begin{frame}[fragile]
3241
+
\frametitle{Creating a Finite Number of Threads}
3242
+
\inputminted{rust}{./code/http3.rs}
3243
+
\end{frame}
3244
+
3245
+
\begin{frame}[fragile]
3246
+
\frametitle{Creating a Finite Number of Threads(2)}
3247
+
\inputminted{rust}{./code/http4.rs}
3248
+
\end{frame}
3249
+
3250
+
\begin{frame}[fragile]
3251
+
\frametitle{Creating a Finite Number of Threads(3)}
The error tells us we can’t call join because we only have a mutable borrow of each worker and join takes ownership of its argument. To solve this issue, we need to move the thread out of the Worker instance that owns thread so join can consume the thread. If Worker holds an \mintinline{rust}|Option<thread::JoinHandle<()>>| instead, we can call the \mintinline{rust}|take| method on the Option to move the value out of the Some variant and leave a None variant in its place. In other words, a Worker that is running will have a Some variant in thread, and when we want to clean up a Worker, we’ll replace Some with None so the Worker doesn’t have a thread to run.
3303
+
3304
+
\end{frame}
3305
+
3306
+
3307
+
\begin{frame}[fragile]
3308
+
\frametitle{Graceful Shutdown and Cleanup, Implementing the Drop Trait on ThreadPool(2)}
0 commit comments