Strategies
There are a couple of different ways to share state in Tokio.
- Guard the shared state with a Mutex.
- Spawn a task to manage the state and use message passing to operate on it.
Generally you want to use the first approach for simple data, and the second approach for things that require asynchronous work such as I/O primitives. In this chapter, the shared state is a HashMap
and the operations are insert
and get
. Neither of these operations is asynchronous, so we will use a Mutex
.
The latter approach is covered in the next chapter.