This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
signal-wrangler/README.md

56 lines
1.2 KiB
Markdown
Raw Normal View History

2019-09-15 04:41:56 +08:00
Signal handler for multithreaded C++ applications on Linux
==========================================================
Signal handler that uses [pthread_sigmask](http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html) and [sigwait](http://man7.org/linux/man-pages/man3/sigwait.3.html).
2019-09-15 04:41:56 +08:00
2019-09-17 00:14:15 +08:00
## Dependencies
* C++17
* Clang or GCC
* linux
* pthread
* cmake (recommended, but optional)
2019-10-13 21:11:06 +08:00
* Catch2 for testing
2019-09-17 00:14:15 +08:00
## Example usage
2020-07-14 03:16:03 +08:00
```C++
2019-09-15 04:41:56 +08:00
{
// Block signals
2019-11-28 01:03:34 +08:00
sgnl::SignalHandler signal_handler({SIGINT, SIGTERM});
// Wait for a signal
int signal_number = signal_handler.sigwait();
// Or, pass a handler
auto handler = [](int signum) {
if( signum == SIGINT )
// continue waiting for signals
return false;
if( signum == SIGTERM )
// stop waiting for signals
2019-09-15 04:41:56 +08:00
return true;
};
int last_signal = signal_handler.sigwait_handler(handler);
} // signals are unblocked again
```
2019-10-13 21:06:50 +08:00
See [example.cpp](example/example.cpp) for an example using threads.
## Build & Install
2020-07-14 03:16:03 +08:00
```SH
mkdir -p build/ && cd build/
cmake ..
# build and run tests
make sgnl-test && ./test/sgnl-test
2019-10-13 21:06:50 +08:00
# build and run example
make example && ./example
# install headers and CMake config
make install
2019-09-15 04:41:56 +08:00
```