4

There is a limited number of functions that POSIX defines as async-signal safe. See here.

This includes some fundamental functions such as write.

Since this is really a C standard (since unix is built on C) that makes no mention of C++, I would expect this to be undefined, but the question is: can I use any c++ functions or classes (which involve constructors/destructors etc) inside a signal handler? For example: can I construct a std::string or std::pair?

malloc is not on the list of async-signal safe functions, so it is probably safe to assume that the potential memory allocation used by std::string makes it async-signal unsafe in the general sense (?).

7
  • 2
    Since a signal can interrupt program execution at any point, it takes effort to make arbitrary code execution with data structures (such as used by a heap manager) that could be in a transient state that (temporarily) violates the structure invariants much more difficult. The list of async-signal safe functions for C is also the list of async-signal safe functions for C++. Commented Mar 30 at 11:54
  • so we can't use the subset of c++ that is not C with any guarantees for async-signal safety Commented Mar 30 at 13:28
  • The subset of C++ that is not C which provides async-signal safety guarantees is an empty set. Commented Mar 30 at 14:01
  • 2
    @Eljay This ("... an empty set") is incorret. E.g. all functions from numeric_limits and type_traits are signal-safe. The set is perhaps not particularly useful, but it's not empty. Commented Mar 30 at 14:41
  • 1
    @Eljay By way of example: numeric.limits#members-1 Each member function defined in this subclause is signal-safe ([support.signal]). Commented Mar 30 at 18:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.