C ++,set_terminate局部到每个线程? [英] C++, is set_terminate local to every thread?

查看:245
本文介绍了C ++,set_terminate局部到每个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为C ++ 2011或C ++ 2003中的多个线程设置不同的终止异常处理程序应该 set_terminate / get_terminate / p>

例如如果我有程序并设置终止处理程序 func_1 ;然后我开始3个线程。什么是在新线程中终止处理程序?如果在每个线程我将设置终止处理程序到 func_2 在第一个线程, func_3 在第二个线程等。



[support.exception]中没有任何关于它的信息nofollow> N3242 / [exception.terminate]



PS:对于这些标准的任何流行的实现



PPS:有FCD评论... C ++ FCD评论状态Rev. 5 N3249(2011)

  GB 71 18.6.2.4 / 18.8.2.2 / 18.8.3.2 




std :: set_new_handler()的线程安全 std :: set_unexpected() std :: set_terminate(),使得函数不能以线程安全的方式使用。



必须指定函数的线程安全保证,并且应提供新的接口,以便以线程安全的方式查询和安装处理程序。



LWG 1365 接受修改



请参阅paper N3189



解决方案

17.6.4.7p4说:


调用 set _ * get _ * 函数不会引发数据竞争。对任何 set _ * 函数的调用应与后续对同一 set _ * 函数的调用同步, get _ * 函数。


这意味着 set _ * get _ * 函数在相同的全局状态下运行,即使从不同的线程调用。 18.8.3下的所有段落讨论当前处理程序函数,没有其他提到的线程;这表明处理函数是整个程序的一个属性;类似的,17.6.4.7有:


2 - C ++程序可能在执行期间安装不同的处理函数[...]
3 - 一个C ++程序可以通过调用以下函数来获取当前处理函数的指针[...]


这些段落讨论了程序上下文中的当前处理程序函数,表明它是程序范围的,而不是线程本地的。


Should set_terminate/get_terminate set a different terminate exception processor for several threads in C++ 2011 or C++ 2003?

E.g. if I have program and sets terminate handler to func_1; then I start 3 threads. What are terminate handlers in new threads? What if in every thread I will set terminate handler to func_2 in first thread, func_3 in second thread and so on.

N3242 (C++ 2011 draft) says nothing about it in [handler.functions] or in [support.exception]/[exception.terminate]

PS: You may answer for C++2011 or for C++2003 of for any popular implementation of these standards

PPS: There is FCD Comment for this... C++ FCD Comment Status Rev. 5 N3249 (2011):

GB 71    18.6.2.4 / 18.8.2.2 / 18.8.3.2   

The thread safety of std::set_new_handler(), std::set_unexpected(), std::set_terminate(), is unspecified making the the functions impossible to use in a thread safe manner.

The thread safety guarantees for the functions must be specified and new interfaces should be provided to make it possible to query and install handlers in a thread safe way.

LWG 1365 ACCEPTED with MODIFICATIONS

See paper N3189

解决方案

17.6.4.7p4 says:

Calling the set_* and get_* functions shall not incur a data race. A call to any of the set_* functions shall synchronize with subsequent calls to the same set_* function and to the corresponding get_* function.

This strongly implies that the set_* and get_* functions are operating on the same global state even when called from different threads. All the paragraphs under 18.8.3 discuss "the current handler function", with no other mention of threading; this indicates that the handler function is a property of the program as a whole; similarly, 17.6.4.7 has:

2 - A C++ program may install different handler functions during execution [...]
3 - A C++ program can get a pointer to the current handler function by calling the following functions [...]

These paragraphs discuss the current handler function in the context of a program, indicating that it is program-scope and not thread-local.

这篇关于C ++,set_terminate局部到每个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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