omni::application::signal_handler::attach

SYNOPSIS

Attaches a delegate to the underlying system signal handler.

top

DESCRIPTION

Attaching to the application signal handler will allow you to listen for signals sent by the system. These can be signals like SIGABRT, SIGSEGV, or SIGINT, but can also be user signals sent via the system's signal command (some platforms can issue specific signals). When a delegate is attached it will be invoked in the order it was attached.

top

PARAMETERS

sig_func - The omni::application::signal_handler::callback function delegate to attach to the signal handler event.

top

RETURN VALUES

No return value.

top

ERRORS

If an exception occurs on an attached delegate, it will be handled according to omni::sync::user_thread_exception and omni::sync::unhandled_thread_exception.

top

CONSIDERATIONS

This function will only have an effect if you have explicitly called one of the omni::application::run functions to block the main thread until program completion.

top

PLATFORM SPECIFIC

On Windows based platforms, this will listen for the system signal events (as described), as well as set the console control handler, which can handle other signals as well (such as the CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT).

top

NOTES

Since the signals can be raised on different threads, it's important to take care of any multi-threaded issues that might arise when using any of the signal handlers. Ensure the functions are re-entrant.

top

EXAMPLE
#include <omni/application>

static volatile bool dorun = true;

void app_signal(int sig)
{
    std::cout << "Signal received: " << sig << std::endl;
    dorun = false;
}

void app_run()
{
    while (dorun) {
        omni::sync::sleep(10); // small sleep
    }
}

int main(int argc, char* argv[])
{
    omni::application::signal_handler::attach(&app_signal);
    return omni::application::run(&app_run);
}
Visit the examples page for more.

top