mosra / magnum

Lightweight and modular C++11 graphics middleware for games and data visualization
https://magnum.graphics/
Other
4.75k stars 439 forks source link

Feature Request: Support std::stop_token as optional parameter to Sdl2Application::exec() #566

Closed jonesmz closed 1 year ago

jonesmz commented 2 years ago

See: https://en.cppreference.com/w/cpp/thread/stop_token

And Sdl2Application::exec() is implemented as follows:

int Sdl2Application::exec() {
    #ifndef CORRADE_TARGET_EMSCRIPTEN
    while(mainLoopIteration()) {}
    #else
    emscripten_set_main_loop_arg([](void* arg) {
        static_cast<Sdl2Application*>(arg)->mainLoopIteration();
    }, this, 0, true);
    #endif
    return _exitCode;
}

It would be nice, if Magnum was compiled with C++20, to have the ability to pass an std::stop_token into the exec() function as an additional way to terminate.

This would be an alternative to calling Sdl2Application::exit();

The motivation behind this feature request is to support slightly better encapsulation, where an application that has a threadpool, holding one or more std::jthread objects may not want to have the threadpool code aware of special rules to shutdown the threads, allowing instead for the std::stop_token to be respected directly for a pool-wide graceful shutdown.

mosra commented 2 years ago

Hm, couldn't you use mainLoopIteration() directly in that case? Completely with your own main() as well, since the MAGNUM_APPLICATION_MAIN() calls exec().

There's a lot of use cases where exec() isn't flexible enough and that's why mainLoopIteration() is exposed as a separate, lower-level API -- you're not required to call it, and you're not required to use the MAGNUM_APPLICATION_MAIN() macro either. The only bit of friction is on Emscripten, but there you can't reliably use threads anyway (and IIRC, OSP doesn't have a web build at the moment?).

mosra commented 1 year ago

Not planned, use mainLoopIteration() directly instead.