Skip to content

Is it available to use Rc<RefCell<>> in spawned tasks #4371

Answered by Darksonn
ActivePeter asked this question in Q&A
Discussion options

You must be logged in to vote

In general, a Tokio task may be moved from one thread to another at any .await, but it is not safe to move an Rc between threads. So to use an Rc in a spawned task, you must do one of the following:

  1. Make sure that the Rc is sufficiently short-lived that it does not have to exist when you perform an .await.
  2. Spawn your task on a LocalSet so that it can't be moved between threads.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ActivePeter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants