Class Once

Synopsis

#include <src/uvw/thread.h>

class Once final: public UnderlyingType<Once, uv_once_t>

Description

The Once wrapper.

Runs a function once and only once. Concurrent calls to once will block all callers except one (it’s unspecified which one).

Inheritance

Ancestors: UnderlyingType

Methods

onceRuns a function once and only once.
UnderlyingType overload

Source

Lines 151-173 in src/uvw/thread.h.

class Once final: public UnderlyingType<Once, uv_once_t> {
    static uv_once_t* guard() noexcept;

public:
    using UnderlyingType::UnderlyingType;

    /**
     * @brief Runs a function once and only once.
     *
     * The callback must be such that it's convertible to `void(*)(void)`. Free
     * functions and non-capturing lambdas are both viable solutions.
     *
     * @tparam F Type of the callback.
     * @param f A valid callback function.
     */
    template<typename F>
    static void once(F &&f) noexcept {
        using CallbackType = void(*)(void);
        static_assert(std::is_convertible_v<F, CallbackType>);
        CallbackType cb = f;
        uv_once(guard(), cb);
    }
};





Add Discussion as Guest

Log in