Is pthread_spin_trylock safe inside of sigsegv handler of multithreaded application?

Posted by TWMouton on Stack Overflow See other posts from Stack Overflow or by TWMouton
Published on 2012-08-24T16:37:33Z Indexed on 2012/08/29 21:39 UTC
Read the original article Hit count: 154

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about linux

Related posts about pthreads