What's the best practice in Rust to cancel multiple threads/tasks?

77 points zplCoder

Say if I have a async library, when the client invokes \start\\, I will start 3 tasks with tokio::spawn in the background, when \stop\\ was called, I want to stop all 3 tasks.

I'm from Golang and it's common to use \context.Context\\ for this kind of job, I searched around and I think I can use broadcast, if the client invokes \stop\\, I can close the channel and other tasks can be notified.

I'm not sure if this is the right way to do it, any suggestion is welcome!