Function getDefault

Synopsis

#include <src/uvw/loop.h>

static std::shared_ptr< Loop > getDefault()

Description

Gets the initialized default loop.

It may return an empty pointer in case of failure.
This function is just a convenient way for having a global loop throughout an application, the default loop is in no way different than the ones initialized with create().
As such, the default loop can be closed with close() so the resources associated with it are freed (even if it is not strictly necessary).

Returns
The initialized default loop.

Mentioned in

Source

Lines 37-55 in src/uvw/loop.cpp. Line 120 in src/uvw/loop.h.

UVW_INLINE std::shared_ptr<Loop> Loop::getDefault() {
    static std::weak_ptr<Loop> ref;
    std::shared_ptr<Loop> loop;

    if(ref.expired()) {
        auto def = uv_default_loop();

        if(def) {
            auto ptr = std::unique_ptr<uv_loop_t, Deleter>(def, [](uv_loop_t *) {});
            loop = std::shared_ptr<Loop>{new Loop{std::move(ptr)}};
        }

        ref = loop;
    } else {
        loop = ref.lock();
    }

    return loop;
}





Add Discussion as Guest

Log in