Is thread_local to be avoided? #4606
-
|
I've had such so many problems with my new app ssr/hydrate (anything I do will bail out on lack of Sync + Send) that I ended up with most stuff in thread_local, and most (now maybe even all) ended up as web-sys calbacks. I don't know why. My previous app had none of these problems, probably because of unsafe impl Sync + Send on some critical types, which is something I want to avoid. My question is, should I avoid thread_local? And if so, what is the pattern to get around my Sync+Send problems? I ended up with Send+Sync problems even when using StoredValue::new_local and then tried to provide_context(). Not immediately, but later when trying to read the value after expect_context(). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
I don't think it's bad, but you probably don't need it. For browser-only types consider using Feel free to provide example code that's been problematic. |
Beta Was this translation helpful? Give feedback.
-
|
Here's my latest attempt, where I gave up. I am receiving this:
With other types, I had problems with Get (for other types) and so on. thread_local solved it. And I know that if I drop Rc, I will not get this error, but I need Rc (at least I think I need it). What I do know is that I'm at the very edge of my Rust skills, that's for sure. I'm just confused why my previous app, which also did wgpu and had multiple canvases, gave me none of those problems, and now, it's becoming all web-sys (in which case, I most probably don't have the skills to finish). |
Beta Was this translation helpful? Give feedback.
You can't use
Rc<RefCell<_>>with the reactive system, which requiresSend + Syncvalues. Sorry.I don't know what
IllumCanvasHandleis. If it's a threadsafe type, then you can use it withArc<Mutex<_>>if you need interior mutability.Note that
StoredValuealready provides interior mutability, though; there's almost never a reason to use an Arc/Rc inside it.