Chapter 20. Asynchronous Programming
In the last chapter, we explained how to use threads in Rust to spread compute-intensive work across multiple processor cores, putting the entire machine to work. In this chapter and the next, we’ll present an alternative to threads, offering more efficient concurrency for I/O-bound programs.
For CPU-bound programs, like our Mandelbrot example, the performance hinges entirely on efficient use of the available CPU time. The job requires so much processing that no other resource is under load. Threads are very good at putting CPUs to work.
A network service, though, has very different performance characteristics. If it spawns a thread to handle each incoming request, that thread will spend almost all of its time blocked, waiting for I/O. The amount of CPU needed is so small that a single server can serve thousands or even hundreds of thousands of concurrent requests. Performance then depends on efficient use of memory and efficient task creation and switching. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access