Skip to content

Why can't RefCells be used inside (non-local) tasks? #4702

Answered by Darksonn
oliver-giersch asked this question in Q&A
Discussion options

You must be logged in to vote

You are correct that having two halves of a tokio::join! access the same RefCell would be fine, because even if the task gets moved across threads, both shared references to the RefCell are moved together. You never end up with two different threads both having a reference to the same RefCell 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".

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@oliver-giersch
Comment options

@Darksonn
Comment options

@oliver-giersch
Comment options

@Darksonn
Comment options

@oliver-giersch
Comment options

Answer selected by oliver-giersch
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