Comment by tuxoko
4 years ago
The suggestions here aren't really great. What you should do is already written in the fork(2) manpage https://man7.org/linux/man-pages/man2/fork.2.html
"After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions (see signal-safety(7)) until such time as it calls execve(2)."
So just use only async-singal-safe function https://man7.org/linux/man-pages/man7/signal-safety.7.html
I don't know why so many people still hit this issue when it already told you what you can do and not do in the document. I've done this sort of things without any issue.
Because in practice it’s almost impossible to know if you are single threaded still. Plenty of system libraries have background threads.
Not to disbelieve you but which ones?
There are many languages where it's difficult to impossible to be async-signal-safe because you can't avoid memory allocations.