3 ms·
How maintainable are these hacks in the long run? Is there firm theoretical basis, if yes, then sources?
by denfromufa 10y ago
How maintainable are these hacks in the long run? Is there firm theoretical basis, if yes, then sources?
- pizlonator 10y agoThe lazy initialization example is a classic algorithm. Usually concurrency hackers rediscover it from first principles. It's easy to maintain because the concurrency protocol it uses is easy to understand. Here's how I'd write it: template<typename T, typename... Args> void constructSingleton(Atomic<T*>& singletonRef, Args&&... args) { for (;;) { T* oldValue = singletonRef.load(); if (oldValue) return oldValue; T* newValue = new T(std::forward<Args>(args)...); if (singletonRef.compareExchangeWeak(nullptr, newValue)) return newValue; delete newValue; } } This works so long as the singletons are happy to be deleted. I'm assuming that's true here.
- denfromufa 10y agoI think this is C++11, while CPython is using C89 and may update to use some C99 features supported by major compilers (GCC, Clang, MSVC).
- pizlonator 10y agoThen write it in C89. It's not hard. My first lock-free algo was in C89.