https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104495

--- Comment #5 from Frank Heckenbach <f.heckenb...@fh-soft.de> ---
As I replacement, I'll use the following code. It's a simple double-checked
lock, probably not as efficient as the original, but seems to work.

Posted here as RFC and for anyone else who encounters the problem. Released
under CC0/public domain.

#include <mutex>
#include <atomic>
#include <functional>

class once_flag
{
  std::atomic <bool> a { };
  std::mutex m;
public:
  once_flag () = default;
  once_flag (once_flag &) = delete;
  friend void call_once (once_flag &o, auto &&f, auto && ... args)
  {
    if (!o.a)
      if (std::lock_guard g (o.m); !o.a)
        {
          std::invoke (std::forward <decltype (f)> (f), std::forward <decltype
(args)> (args) ...);
          o.a = true;
        }
  }
};

Reply via email to