Class Thread

Synopsis

#include <src/uvw/thread.h>

class Thread final: public UnderlyingType<Thread, uv_thread_t>

Description

The Thread wrapper.

To create a Thread through a Loop, arguments follow:

  • A callback invoked to initialize thread execution. The type must be such that it can be assigned to an std::function<void(std::shared_ptr<void>)>.
  • An optional payload the type of which is std::shared_ptr<void>.

Inheritance

Ancestors: UnderlyingType

Methods

Thread
~Thread
equalCompares thread by means of their identifiers.
joinJoins with a terminated thread.
run overloadCreates a new thread.
selfObtains the identifier of the calling thread.

Source

Lines 49-107 in src/uvw/thread.h.

class Thread final: public UnderlyingType<Thread, uv_thread_t> {
    using InternalTask = std::function<void(std::shared_ptr<void>)>;

    static void createCallback(void *arg);

public:
    using Options = details::UVThreadCreateFlags;
    using Task = InternalTask;
    using Type = uv_thread_t;

    explicit Thread(ConstructorAccess ca, std::shared_ptr<Loop> ref, Task t, std::shared_ptr<void> d = nullptr) noexcept;

    /**
     * @brief Obtains the identifier of the calling thread.
     * @return The identifier of the calling thread.
     */
    static Type self() noexcept;

    /**
     * @brief Compares thread by means of their identifiers.
     * @param tl A valid instance of a thread.
     * @param tr A valid instance of a thread.
     * @return True if the two threads are the same thread, false otherwise.
     */
    static bool equal(const Thread &tl, const Thread &tr) noexcept;

    ~Thread() noexcept;

    /**
     * @brief Creates a new thread.
     * @return True in case of success, false otherwise.
     */
    bool run() noexcept;

    /**
     * @brief Creates a new thread.
     *
     * Available flags are:
     *
     * * `Thread::Options::THREAD_NO_FLAGS`: no flags set.
     * * `Thread::Options::THREAD_HAS_STACK_SIZE`: if set, `stack` specifies a
     * stack size for the new thread. 0 indicates that the default value should
     * be used (it behaves as if the flag was not set). Other values will be
     * rounded up to the nearest page boundary.
     *
     * @return True in case of success, false otherwise.
     */
    bool run(Flags<Options> opts, std::size_t stack = {}) noexcept;

    /**
     * @brief Joins with a terminated thread.
     * @return True in case of success, false otherwise.
     */
    bool join() noexcept;

private:
    std::shared_ptr<void> data;
    Task task;
};





Add Discussion as Guest

Log in