# How to share tokio JoinSet across different tasks/threads?

**URL:** <https://users.rust-lang.org/t/how-to-share-tokio-joinset-across-different-tasks-threads/117289>\
**Category:** help\
**Created:** [September 9, 2024, 9:12am UTC](https://users.rust-lang.org/t/how-to-share-tokio-joinset-across-different-tasks-threads/117289 "2024-09-09T09:12:04Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [September 9, 2024, 10:28am UTC](https://users.rust-lang.org/t/how-to-share-tokio-joinset-across-different-tasks-threads/117289/3 "2024-09-09T10:28:46Z")

</div>

A few tips:

- You probably should not be using `JoinSet` here. Instead I recommend [`CancellationToken`](https://docs.rs/tokio-util/latest/tokio_util/sync/struct.CancellationToken.html) for being able to abort tasks, and optionally [`TaskTracker`](https://docs.rs/tokio-util/latest/tokio_util/task/task_tracker/struct.TaskTracker.html) for being able to wait for them to exit / wait for abort to finish. See [graceful shutdown](https://tokio.rs/tokio/topics/shutdown) for more info on this.
- As a general tip, trying to share something with `&'static` or `&'static mut` is always always the wrong way, every time.
  - In the case of `JoinSet`, this type does _not_ come with any utilities for sharing it.
  - You could still do so. Generally, I recommend using [the actor pattern](https://ryhl.io/blog/actors-with-tokio/) for sharing values for most cases.
  - However, if you switch to using `TaskTracker`, then it _does_ come built-in with utilities for making it easier to share. Specifically, you can share it by cloning it. See [the documentation](https://docs.rs/tokio-util/latest/tokio_util/task/task_tracker/struct.TaskTracker.html#impl-Clone-for-TaskTracker) for more info. The same applies to the cancellation token.

Even though I recommend you switch to `TaskTracker` instead of `JoinSet`, you can still benefit from the actor pattern with your application. I strongly recommend reading my article about it. Also, the talk about actors (linked in the article) was done _after_ the article, and has more tips than the article itself, so I also recommend my talk.

---

_[View the full topic](https://users.rust-lang.org/t/how-to-share-tokio-joinset-across-different-tasks-threads/117289)._
