5 ms·
Though I have struggled with this quite a few times, I have never considered that preventing your code from being called from multiple threads is a great featur
by dxuh 4y ago
Though I have struggled with this quite a few times, I have never considered that preventing your code from being called from multiple threads is a great feature to have. I have written comments stating that certain functions should not be called from multiple threads many times. And I myself have called these functions from multiple threads by accident before.
Just another comment about this:
> Basically all languages except C(++) have an async mode that transforms code into state machines that can be interleaved on a single thread of execution.
C++ has coroutine support since C++20. It's not pretty, but it's there.
- lll-o-lll 4y agoNot pretty! How dare you! Beauty is clearly in the eye of the beholder.
- masklinn 4y agoOr the numerous eyestalks of the Beholder, as the case may be.
- throwaway17_17 4y agoI know meta-commentary on comments are slightly frowned upon, but yours just made my morning. So sincerely, thank you.
- nly 4y agoC++ coroutines are incomplete without a an executor to run them on. Unlike C# you can't just sprinkle in async and await keywords and let the background thread pool take care of it...and doing so would be undesirable, given the lack of thread safe garbage collection. Boost.Fiber on the other hand pretty damn cool.
- steveklabnik 4y agoThe exact same thing is true of Rust.
- hedora 4y agoThat is a feature of coroutines, not a bug. I’ve written executors in multiple languages. For production code, it is usually easier than reusing some complicated thing that is a poor fit but does nothing well.
- midjji 4y agoThere really should be a syntactic sugar for things like pure function/stateless, and its collary, explicitly reentrant, verified by the compiler. Then a debug mode- or perhaps flag would allow all functions which dont qualify as explicitly non-threadsafe, possibly having that as the default. But I'm dubious about the use of needing to explicitly mark functions as non-threadsafe, its a bit like the const vs mut, but while const might be slightly less common save for things that should be autogenerated, in the case of reentrant, if it isnt explicitly marked, it should never be used reentrant, even if it happens to be a pure function at the moment, or for this one implementation etc.