Tokio
Q n A
- What does
Send
mean?Send
means that when.await
suspends the thread, the result can be moved to another thread (the caller thread).Rc
is a type that cannot beSend
.Sync
means a type can be concurrently accessed through immutable references.Cell
isSend
but notSync
because it can be modified through an immutable reference. - What's the benefit of
bytes::Bytes
overVec<u8>
?clone()
onBytes
does not clone the data. It is roughly anArc<Vec<u8>>
. - Difference between sync mutex
std::sync::Mutex
and async mutexttokio::sync::Mutex
? Sync mutex will block current thread while waiting but async mutex won't. - The problem with sync mutex is that when multiple threads are contending (i.e. competing) for a mutex, only one will be working while others are all waiting. To solve this problem:
- Switching to a dedicated task to manage state and use message passing
- shard the mutex
- restructure code to avoid mutex
- Sending with oneshot channel does not require await. Result of success or failure is known immediately.
- rust async functions are state machines. It will check the contained future's
poll()
and decide if go to the next state, or check back later.
Stream
Follow this tutorial to learn how to create a stream. Especially the implementation of into_stream()