必须由用户提供的terminate()函数是线程安全的吗? [英] Must user provided terminate() function be thread-safe?

查看:229
本文介绍了必须由用户提供的terminate()函数是线程安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://en.cppreference.com/w/cpp/error/terminate有很多原因要调用terminate。我可以想象几乎在同一时间一些这些原因发生在两个线程的情况。



Q1 可以调用 std :: set_terminate 设置的终止函数两次更多的同时,同时我的意思是第二个调用在第一个结束之前开始。

  Thread1 Thread2 
| |
_ |
t |
| |
r |
m |
i _
n t
a e
t r
e m
- ?

Q2 如果Q1 == YES, 。我想如果它以std :: abort结束,然后程序结束,但如果用户提供的终止不会中止程序会发生什么?



Q3 是在 std :: set_terminate 导致这个终止调用的线程?

解决方案

Q1



std :: terminate 可以同时调用。



Q2 b
$ b

标准说, terminate_handler 不是不返回调用者而终止程序的执行的未定义的行为。在我熟悉的实现中,如果 terminate_handler 尝试正常或异常返回, abort()



由<$ c $设定的函数

c> std :: terminate
是一个全局,而不是线程局部。



在C ++ 98/03中, terminate_handler $ c> terminate 被调用是因为未捕获的异常是在抛出异常时生效的异常,而不是在终止实际上被调用(虽然它们通常是相同的)。



在C ++ 11中,这被改变了,标准现在说使用的处理程序是一个当时 terminate 被调用。这种变化是错误的,很可能在未来的草案中得到纠正。以下是跟踪此问题的LWG问题:



http://cplusplus.github.com/LWG/lwg-active.html#2111



更新



在2015年春季会议上,LWG决定对现有的行为进行标准化,并在新的 terminate_handler 在堆栈展开期间调用 set_terminate 时生效。也就是说实现允许遵循C ++ 98/03规则或C ++ 11规则。



为了使代码可移植,如果你需要设置一个 terminate_handler ,在程序启动期间,在任何异常被抛出之前,并且不要习惯在调用 set_terminate


As stated in http://en.cppreference.com/w/cpp/error/terminate there are many reasons to call terminate. I can imagine case where just almost in the same time some of these reasons happen in two threads.

Q1 Can the terminate function set by std::set_terminate be called twice or more at the same time, by the same time I mean second call begins before first has ended.

  Thread1   Thread2
    |          |
    _          |
    t          |
    e          |
    r          |
    m          |
    i          _
    n          t
    a          e
    t          r
    e          m
    -          ?

Q2 If Q1==YES, then what happens if first terminate ended. I guess if it ended with std::abort, then program ends, but what happens if user provided terminate does not abort program?

Q3 Is the terminate function set by std::set_terminate called in context of the thread which caused this terminate call?

解决方案

Q1

Yes, std::terminate can be called concurrently.

Q2

The standard says it is undefined behavior for a terminate_handler to not "terminate execution of the program without returning to the caller". In implementations I'm familiar with, if the terminate_handler attempts to return, either normally, or exceptionally, abort() will be called.

Q3

The function set by std::terminate is a global, not a thread local. So one thread can impact another.

In C++98/03, the terminate_handler used when terminate is called due to an uncaught exception is the one that was in effect when the exception was thrown, not the one in effect when terminate is actually called (though they are usually the same).

In C++11 this was changed and the standard now says that the handler used is the one in place at the time terminate is called. This change was done by mistake and will most likely be corrected in a future draft. Here is the LWG issue tracking this issue:

http://cplusplus.github.com/LWG/lwg-active.html#2111

Update

At the Spring 2015 meeting in Lenexa, KS, the LWG decided to standardize existing behavior and made it unspecified when a new terminate_handler goes into effect if set_terminate is called during stack unwinding. I.e. implementations are allowed to follow either the C++98/03 rules or the C++11 rules.

To make your code portable, if you need to set a terminate_handler, do so during program startup, before any exceptions will be thrown, and do not make a habit of calling set_terminate after that.

这篇关于必须由用户提供的terminate()函数是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆