All Projects → thomastrapp → signal-wrangler

thomastrapp / signal-wrangler

Licence: MIT License
Signal handler for multi threaded C++ applications on Linux

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to signal-wrangler

sigctx
Go contexts for graceful shutdown
Stars: ✭ 55 (+161.9%)
Mutual labels:  signals
dot
Personal and professional dotfiles to setup my personal workspace on any Debian/Ubuntu system. Also available as rwxrob/workspace container.
Stars: ✭ 152 (+623.81%)
Mutual labels:  posix
tonix
Tonix provides basic file system functionality, as well as an interactive shell with a Unix-style command line interface.
Stars: ✭ 20 (-4.76%)
Mutual labels:  posix
gitpack
Git-based package manager written in POSIX shell
Stars: ✭ 72 (+242.86%)
Mutual labels:  posix
FP
Simple FontPreview Shell Script
Stars: ✭ 17 (-19.05%)
Mutual labels:  posix
nyx
Lean linux and OSX process monitoring written in C
Stars: ✭ 24 (+14.29%)
Mutual labels:  posix
dotfiles
Portable configuration files and utility scripts for Linux and OpenBSD
Stars: ✭ 29 (+38.1%)
Mutual labels:  posix
dicodile
Experiments for "Distributed Convolutional Dictionary Learning (DiCoDiLe): Pattern Discovery in Large Images and Signals"
Stars: ✭ 15 (-28.57%)
Mutual labels:  signals
beeos
A simple "Unix-like" kernel trying to be POSIX compliant
Stars: ✭ 103 (+390.48%)
Mutual labels:  posix
kernel
Main kernel tree
Stars: ✭ 28 (+33.33%)
Mutual labels:  posix
theBookOfNoah
Everything ive learned developing web applications
Stars: ✭ 22 (+4.76%)
Mutual labels:  posix
cervit
Minimal, multi-threaded POSIX HTTP 1.1 server written in C using only system libraries.
Stars: ✭ 19 (-9.52%)
Mutual labels:  posix
energymech
EnergyMech IRC Bot
Stars: ✭ 24 (+14.29%)
Mutual labels:  posix
posix
POSIX/C bindings generator for the Crystal programming language
Stars: ✭ 32 (+52.38%)
Mutual labels:  posix
shup
A POSIX shell script to parse HTML
Stars: ✭ 28 (+33.33%)
Mutual labels:  posix
matrix multiplication
Parallel Matrix Multiplication Using OpenMP, Phtreads, and MPI
Stars: ✭ 41 (+95.24%)
Mutual labels:  pthreads
vim-filetype-formatter
Format program files in vim using your favorite command line formatter
Stars: ✭ 21 (+0%)
Mutual labels:  posix
signal.lua
Signals are a light-weight messaging tools for Lua and the Corona SDK.
Stars: ✭ 15 (-28.57%)
Mutual labels:  signals
scr
🎤 A Super CRappy SCReenshot & SCreen Recording SCRipt for Sound Cloud Rappers + audio recorder, yes (sponsored by https://git.io/kiwmi)
Stars: ✭ 16 (-23.81%)
Mutual labels:  posix
kfc
A terminal-emulator color palette setter written in POSIX C99.
Stars: ✭ 25 (+19.05%)
Mutual labels:  posix

Signal handler for multi threaded C++ applications on Linux

Signal handler that uses pthread_sigmask and sigwait.

Dependencies

  • C++17
  • Clang or GCC
  • linux
  • pthread
  • cmake (recommended, but optional)
  • Catch2 for testing

Example usage

{
  // Block signals
  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
      return true;
  };

  int last_signal = signal_handler.sigwait_handler(handler);
} // signals are unblocked again

Using a condition variable to shutdown all threads:

#include <sgnl/AtomicCondition.h>
#include <sgnl/SignalHandler.h>

#include <cstdlib>
#include <future>
#include <iostream>
#include <thread>


void Worker(const sgnl::AtomicCondition<bool>& exit_condition)
{
  auto predicate = [&exit_condition]() {
    return exit_condition.get();
  };
  while( true )
  {
    exit_condition.wait_for(std::chrono::minutes(1), predicate);
    if( exit_condition.get() )
      return;
    /* ... do work ... */
  }
}

int main()
{
  sgnl::AtomicCondition<bool> exit_condition(false);

  auto handler = [&exit_condition](int signum) {
    std::cout << "received signal " << signum << "\n";
    if( signum == SIGTERM || signum == SIGINT )
    {
      exit_condition.set(true);
      // wakeup all waiting threads
      exit_condition.notify_all();
      // stop polling for signals
      return true;
    }

    // continue waiting for signals
    return false;
  };

  // Block signals in this thread.
  // Threads spawned later will inherit the signal mask.
  sgnl::SignalHandler signal_handler({SIGINT, SIGTERM, SIGUSR1});

  std::future<int> ft_sig_handler =
    std::async(
        std::launch::async,
        &sgnl::SignalHandler::sigwait_handler,
        &signal_handler,
        std::ref(handler));

  std::vector<std::future<void>> futures;
  for(int i = 0; i < 10; ++i)
    futures.push_back(
        std::async(
          std::launch::async,
          Worker,
          std::ref(exit_condition)));

  // SIGUSR1
  std::this_thread::sleep_for(std::chrono::milliseconds(100));
  kill(0, SIGUSR1);

  // SIGTERM
  kill(0, SIGTERM);
  std::this_thread::sleep_for(std::chrono::milliseconds(100));

  for(auto& future : futures)
    future.wait();

  int last_signal = ft_sig_handler.get();
  std::cout << "exiting (received signal " << last_signal << ")\n";

  return EXIT_SUCCESS;
}

Build & Install

mkdir -p build/ && cd build/
cmake ..
# build and run tests
make sgnl-test && ./test/sgnl-test
# build and run example
make example && ./example
# install headers and CMake config
make install

Using signal-wrangler with CMake

The easiest way to add signal-wrangler to a CMake project is by using FetchContent:

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(my-example)

include(FetchContent)
FetchContent_Declare(
  signal-wrangler
  GIT_REPOSITORY https://github.com/thomastrapp/signal-wrangler
  GIT_TAG v0.4.0)
FetchContent_MakeAvailable(signal-wrangler)

add_executable(my-example "example/example.cpp")
target_link_libraries(my-example sgnl::sgnl)

Or, by installing signal-wrangler (make install) and using find_package:

find_package(Sgnl REQUIRED)
target_link_libraries(my-project sgnl::sgnl ...)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].