Say I have a function whose prototype looks like this, belonging to class container_class
:
假设我有一个原型如下所示的函数,属于类container_class:
std::vector<int> container_class::func(int param);
The function may or may not cause an infinite loop on certain inputs; it is impossible to tell which inputs will cause a success and which will cause an infinite loop. The function is in a library of which I do not have the source of and cannot modify (this is a bug and will be fixed in the next release in a few months, but for now I need a way to work around it), so solutions which modify the function or class will not work.
该函数可能会或可能不会在某些输入上产生无限循环;无法确定哪些输入会导致成功,哪些输入会导致无限循环。该函数在一个库中,我没有源代码,也无法修改(这是一个bug,将在几个月内在下一个版本中修复,但是现在我需要一种方法来解决它),所以修改功能或类的解决方案将无法正常工作。
I've tried isolating the function using std::async
and std::future
, and using a while loop to constantly check the state of the thread:
我尝试使用std :: async和std :: future隔离函数,并使用while循环不断检查线程的状态:
container_class c();
long start = get_current_time(); //get the current time in ms
auto future = std::async(&container_class::func, &c, 2);
while(future.wait_for(0ms) != std::future_status::ready) {
if(get_current_time() - start > 1000) {
//forcibly terminate future
}
sleep(2);
}
This code has many problems. One is that I can't forcibly terminate the std::future
object (and the thread that it represents).
这段代码有很多问题。一个是我不能强行终止std :: future对象(以及它所代表的线程)。
At the far extreme, if I can't find any other solution, I can isolate the function in its own executable, run it, and then check its state and terminate it appropriately. However, I would rather not do this.
在极端情况下,如果我找不到任何其他解决方案,我可以在自己的可执行文件中隔离该函数,运行它,然后检查其状态并适当地终止它。但是,我宁愿不这样做。
How can I accomplish this? Is there a better way than what I'm doing right now?
我怎么能做到这一点?有没有比我现在做的更好的方式?
1 个解决方案
#1
2
You are out of luck, sorry.
对不起,你运气不好
First off, C++ doesn't even guarantee you there will be a thread for future execution. Although it would be extremely hard (probably impossible) to implement all std::async
guarantees in a single thread, there is no direct prohibition of that, and also, there is certainly no guarantee that there will be a thread per async
call. Because of that, there is no way to cancel the async execution.
首先,C ++甚至不保证会有一个线程供将来执行。虽然在单个线程中实现所有std :: async保证是非常困难的(可能是不可能的),但没有直接禁止,并且当然也不能保证每个异步调用都会有一个线程。因此,无法取消异步执行。
Second, there is no such way even in the lowest level of thread implementation. While pthread_cancel
exists, it won't protect you from infinite loops not visiting cancellation points, for example.
其次,即使在最低级别的线程实现中也没有这种方式。例如,当pthread_cancel存在时,它不会保护您免受无限循环访问取消点的影响。
You can not arbitrarily kill a thread in Posix, and C++ thread model is based on it. A process really can't be a scheduler of it's own threads, and while sometimes it is a pain, it is what it is.
你不能在Posix中任意杀死一个线程,而C ++线程模型就是以它为基础的。一个进程实际上不能成为它自己的线程的调度程序,虽然有时它是一个痛苦,它就是它的本质。
#1
2
You are out of luck, sorry.
对不起,你运气不好
First off, C++ doesn't even guarantee you there will be a thread for future execution. Although it would be extremely hard (probably impossible) to implement all std::async
guarantees in a single thread, there is no direct prohibition of that, and also, there is certainly no guarantee that there will be a thread per async
call. Because of that, there is no way to cancel the async execution.
首先,C ++甚至不保证会有一个线程供将来执行。虽然在单个线程中实现所有std :: async保证是非常困难的(可能是不可能的),但没有直接禁止,并且当然也不能保证每个异步调用都会有一个线程。因此,无法取消异步执行。
Second, there is no such way even in the lowest level of thread implementation. While pthread_cancel
exists, it won't protect you from infinite loops not visiting cancellation points, for example.
其次,即使在最低级别的线程实现中也没有这种方式。例如,当pthread_cancel存在时,它不会保护您免受无限循环访问取消点的影响。
You can not arbitrarily kill a thread in Posix, and C++ thread model is based on it. A process really can't be a scheduler of it's own threads, and while sometimes it is a pain, it is what it is.
你不能在Posix中任意杀死一个线程,而C ++线程模型就是以它为基础的。一个进程实际上不能成为它自己的线程的调度程序,虽然有时它是一个痛苦,它就是它的本质。