Tokio

Q n A

  1. What does Send mean? Send means that when .await suspends the thread, the result can be moved to another thread (the caller thread). Rcis a type that cannot be Send. Sync means a type can be concurrently accessed through immutable references. Cell is Send but not Sync because it can be modified through an immutable reference.
  2. What's the benefit of bytes::Bytes over Vec<u8>? clone() on Bytes does not clone the data. It is roughly an Arc<Vec<u8>>.
  3. Difference between sync mutex std::sync::Mutex and async mutext tokio::sync::Mutex? Sync mutex will block current thread while waiting but async mutex won't.
  4. 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:
  5. Switching to a dedicated task to manage state and use message passing
  6. shard the mutex
  7. restructure code to avoid mutex
  8. Sending with oneshot channel does not require await. Result of success or failure is known immediately.
  9. 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()