最近在CodeReview.SE上,我看到了一个回答,讲述了一种称为“异常喷吐”的技术。显然,这个技巧用于利用异常必须以线程安全的方式实现,而与编译器是否支持
我在下面粘贴了部分回答内容:
thread_local
变量无关。我在下面粘贴了部分回答内容:
There's an existing technique which is not dissimilar referred to as "exception vomiting". Observe:
void f(void(*p)()) { p(); } template<typename F> void real_f(F func) { try { throw func; } catch(...) { f([] { try { throw; } catch(F func) { func(); } }); } }
This abuses the fact that the compiler must provide a thread-local stack for complex objects for use as exception storage, regardless of their support for other thread local features, and therefore enjoys much broad compiler support. The most obvious drawbacks are a) it's horrible, and b) it's limited to stack semantics.
我的问题是,这个技巧实际上是如何工作的,并且它是否“安全”?