title: Achieve fork-safety for C++ programs
body: <p>I'm an infra engineer, whose main programming language is C++. I expose interface to Python users via <a href="https://github.com/pybind/pybind11" rel="nofollow noreferrer">pybinder</a>. One of my headache derives from: threads in Python don't support parallelism via multi-threading (because of GIL), so have to achieve parallelism by fork/multi-processing, which breaks my program.</p>
  <p>I want to know what should I do to achieve fork-safety? Several things I have tried:</p>
  <ol>
  <li>spawn new threads after fork, by <a href="https://man7.org/linux/man-pages/man3/pthread_atfork.3.html" rel="nofollow noreferrer">atfork</a>.</li>
  <li>close all file descriptors inherited in child process, which are inherited from parent process, and re-open them in the child process. This is also achieve by fork-hook.</li>
  </ol>
  <p>But I cannot make my program fork-safe, a concrete example is: <a href="https://curl.se/libcurl/c/ephiperfifo.html" rel="nofollow noreferrer">https://curl.se/libcurl/c/ephiperfifo.html</a></p>
  <p>I want to know, what else should I do?</p>