Skip to content
Fernando Mendez
TwitterGithub

Rust adventures - Async - Part 1

rust, programming3 min read

A while ago I realized that I was a visual learner which can be frustrating at times since some concepts might take me a bit longer to fully understand until I create the proper mental image(s) for it (or somebody else does it for me).

When I started wrapping my head around Async programming in Rust I felt like I was missing some of those images. What follows is my attempt visualize the concepts around async programing.

Traditional threaded applications

When creating multi-threading applications, if you wanted to execute multiple tasks at the same time you'll need the same number of threads as tasks. So you'll have a 1-1 mapping between tasks and threads. Let's imagine for example, we wanted to execute 3 tasks:

1use std::thread;
2
3pub fn main() {
4 thread::spawn(move || make_coffee_task());
5 thread::spawn(move || waste_time_task());
6 let handle = thread::spawn(move || do_laundry_task());
7 // wait for other threads to complete
8 handle.join().unwrap()
9}
10
11fn make_coffee_task() {
12 // Mocha time!
13 println!("Coffee done");
14}
15
16fn do_laundry_task() {
17 // Mostly gym clothing so it should be quick!
18 println!("Laundry done");
19}
20
21fn waste_time_task() {
22 // Checks twitter...
23 println!("Done wasting time!");
24}

It might looks like this at the Operating System (OS) level1:

During the execution of these threads, any of them can become blocked ( unable to continue to do work ) or they could just voluntarily yield ( programmer knew the thread will have nothing to do for some time under certain condition ), at that point the OS will have to save the state of the running thread (so that it can resume running at a later point) and select another to start or continue running. This is called thread-context switch.

For a variety of reasons2, this context switching has some amount of overhead that penalizes performance. Additionally, the approach doesn't scale well since the more tasks we want to run at the same time the more threads we'll need (eventually running out of resources).

Async

With Asynchronous code instead of using one thread per tasks, we can run multiple tasks concurrently on the same OS thread.

Because multiple tasks are running on the same OS thread, the issue of thread-context switching is greatly improved (less threads to switch around). Furthermore, since we're reusing the same OS threads for multiple tasks, we end-up using significantly less resources.

For both of those reasons our Async counterpart has the potential of being much faster:

1use futures; // Using futures = "0.3.1"
2use std::thread;
3
4async fn async_main() {
5 let coffee_future = make_coffee_task();
6 let laundry_future = do_laundry_task();
7 let waste_future = waste_time_task();
8
9 // Running the futures concurrently within the same thread
10 futures::join!(coffee_future, laundry_future, waste_future);
11}
12
13fn main() {
14 futures::executor::block_on(async_main());
15}
16
17async fn make_coffee_task() {
18 // Mocha time!
19 println!("Using thread Id: {:?} ", thread::current().id());
20 println!("Coffee done! \n");
21}
22
23async fn do_laundry_task() {
24 // Mostly gym clothing so it should be quick!
25 println!("Using thread Id: {:?} ", thread::current().id());
26 println!("Laundry done! \n");
27}
28
29async fn waste_time_task() {
30 // Checks twitter...
31 println!("Using thread Id: {:?} ", thread::current().id());
32 println!("Done wasting time! \n");
33}

This code is meant to act cooperatively in a single thread.

In the next part we'll be exploring the Future trait and the surrounding Async programming ecosystem further.

Footnotes:

Footnotes

  1. Threads

  2. Context Switch

© 2024 by Fernando Mendez. All rights reserved.
Theme by LekoArts