__try / __在UNIX中最终等效 [英] __try/__finally equivalent in UNIX
问题描述
我有一个主要用C语言编写的库,它的某些部分应该是线程安全的,并且我使用全局自旋锁来保护代码的关键部分。
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.
现在我的问题是:
我应该在受自旋锁保护的关键区域内调用用户的一些回调,以防这些回调产生异常,在Windows上我有一个_ try / _finally在C语言中即使出现错误也可以离开关键部分。我也可以在UNIX上完成此操作吗?
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?
推荐答案
这称为结构化异常处理。可以使用 setjmp
和 longjmp
来完成。它已经在C语言中使用了很长时间了。详情请参见: http://www.freetype.org/david/reliable-c。 html
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
这篇关于__try / __在UNIX中最终等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!