ImmutableOctet / glare

Glare: Open Source Game Engine written in Modern C++
MIT License
0 stars 0 forks source link

Implement `on`/`when` shorthand API for event-triggered and condition-dependent sub-threads #84

Closed ImmutableOctet closed 1 month ago

ImmutableOctet commented 1 month ago

This would work similarly to until, but rather than yielding on the current thread, we would instead spin up a sub-thread to wait + perform an action.

API usage would look something like:

on<EventType>
(
    [](auto&& event_instance)
    {
        print("Event received, doing work on sub-thread.");

        // ...
    }
);

on
(
    // Predicate:
    []() -> bool
    {
        return (condition_to_be_met); // Ran on a sub-thread.
    },

    // Work:
    [](auto& self)
    {
        print("Doing work on new sub-thread");

        // ...
    }
)

// NOTE: `on<EventType>(event_predicate_here, some_work_here)` should also work.

This would make sub-threads a lot simpler to use in one-off cases. -- e.g. when waiting for an input that can change or cancel a state.

ImmutableOctet commented 1 month ago

This should now be working as expected, although I decided to go with when for the function name. (May add on later)