Is pthread_spin_trylock safe inside of sigsegv handler of multithreaded application?
- by TWMouton
I am trying to implement a handler that on SIGSEGV will collect some information such as process-id, thread-id and a backtrace and write this information to a file/pipe/socket.
The problem lies in that there is the (probably pretty high) possibility that if one thread experienced a SIGSEGV that the others will shortly follow. If two threads happen to make it to the bit of code where they're writing their report out at the same time then they'll interleave their writing (to the same file).
I know that I should only be using async-signal-safe functions as detailed in signal(7)
I also have seen at least two cases here and video linked in top answer here where others have used pthread_spin_trylock to get around this problem.
Is this a safe way to prevent the above problem?