问题
I have a library that written mostly in C, Some parts of it should be thread safe and I use global spinlock to protect the critical section of the code.
Now my problem is:
I should call some callback of the user from inside of a critical section that protected by a spinlock and in case that those callbacks generate an exception, on Windows I have a _try/_finally in C to be able to leave the critical section even when an error occurred. Can I accomplish this on UNIX too?
回答1:
This is called structured exception handling. It can be accomplished by using setjmp
and longjmp
. It has been used in C for a long time. See this for details: http://www.freetype.org/david/reliable-c.html
来源:https://stackoverflow.com/questions/13001786/try-finally-equivalent-in-unix