#include <atomic> // HACK: <atomic> includes <stdbool.h>, which in some Clang versions does '#define bool bool', // which confuses clang plugins. #undefbool #include <functional>
namespace comphelper
{ /** *Thread-safesingletoncreation. * *Itisnormallysufficienttocreatesingletonsusingstaticvariablesinafunction. *Thisfunctionisonlyforusecasesthathaveamorecomplexlifetimeoftheobject, *suchaswhentheobjectmayrequirespecificcleanupormaybecreatedmoretimes *(e.g.whenthereisa"singleton"pereachinstanceofanotherobject).
*/ template <typename Type, typename Function = std::function<Type*()>, typename Guard = osl::MutexGuard, typename GuardCtor = osl::GetGlobalMutex> staticinline Type* doubleCheckedInit(std::atomic<Type*>& pointer, Function function,
GuardCtor guardCtor = osl::GetGlobalMutex())
{
Type* p = pointer.load(std::memory_order_acquire); if (!p)
{
Guard guard(guardCtor());
p = pointer.load(std::memory_order_relaxed); if (!p)
{
p = function();
pointer.store(p, std::memory_order_release);
}
} return p;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.