Why can't RefCell
s be used inside (non-local) tasks?
#4702
-
This is something I've been pondering over for some time and have not been able to find an answer to. Assume we have a generic async I/O server working roughly like this:
So basically I want to avoid synchronization (so no spawning two sub tasks and wrapping I tried spawning two local tasks and wrapping the shared state in a Is there some other option I for this problem? And also, is there actually a safety reason why |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You are correct that having two halves of a Unfortunately, there's no safe way to do this because the Rust language has no concept of "safe to send as long as you send both halves together". There's only "any kind of sending is safe" and "no kind of sending is safe". |
Beta Was this translation helpful? Give feedback.
You are correct that having two halves of a
tokio::join!
access the sameRefCell
would be fine, because even if the task gets moved across threads, both shared references to theRefCell
are moved together. You never end up with two different threads both having a reference to the sameRefCell
at the same time.Unfortunately, there's no safe way to do this because the Rust language has no concept of "safe to send as long as you send both halves together". There's only "any kind of sending is safe" and "no kind of sending is safe".