3 ms·
Heh, I'm not the only one who always wrote his own async mutex for any non-trivial async codebase. It may seem like single-threaded execution doesn't need mute
by bheadmaster 11mo ago
Heh, I'm not the only one who always wrote his own async mutex for any non-trivial async codebase.
It may seem like single-threaded execution doesn't need mutexes, but it is a fact that async/await is just another implementation of userspace threads. And like all threads, it may lead to data races if you have yield points (await) inside a critical section.
People may say what they want about bad design and reactive programming, but thread model is inherently easier to reason about.
- surajrmal 11mo agoYeah, if you reason about each task as a thread, then all the same problems with sharing data between threads applies to sharing data between tasks, regardless of single threaded runtime or not. The only real difference is you can avoid atomics. Generally, I advocate for avoiding sharing any non-trivial state which would necessitate holding a "lock" across an await point (there are rust lints to help you here if you use RefCell). This usually requires an actor pattern to explicitly queue up work that requires that state into its own self contained task rather than reach for an async mutex.