简体   繁体   English

n秒后杀死提升线程

[英]kill boost thread after n seconds

I am looking for the best way to solve the following (c++) problem. 我正在寻找解决以下(c ++)问题的最佳方法。 I have a function given by some framework, which returns an object. 我有一个框架给出的函数,它返回一个对象。 Sometimes it takes just miliseconds, but on some occasions it takes minutes. 有时它只需几毫秒,但在某些情况下需要几分钟。 So i want to stop the execution if it takes longer than let's say 2 seconds. 所以我想停止执行,如果它需要比说2秒更长的时间。 I was thinking about doing it with boost threads. 我正在考虑使用boost线程来做这件事。 Important sidenote, if the function returns faster than the 2 seconds the program should not wait. 重要的旁注,如果函数返回的速度超过2秒,则程序不应该等待。 So i was thinking about 2 threads: 所以我在考虑2个线程:

1.thread: execute function a       
2.thread: run timer                
if(thread 2 exited bevore thread 1) kill thread 1 
else do nothing

I am struggeling a bit the practical implementation. 我对实际实施有点兴奋。 Especially, 特别,

  • how do i return an object from a child boost thread to the main thread? 我如何将一个对象从子boost线程返回到主线程?
  • how do i kill a thread in boost? 我如何在boost中杀死一个线程?
  • is my idea even a good one, is there a better way to solve the problem in c++ (with or without boost)? 我的想法甚至是一个好的,有没有更好的方法来解决c ++中的问题(有或没有提升)?

As for waiting, just use thread::timed_join() inside your main thread, this will return false , if the thread didn't complete within the given time. 至于等待,只需在主线程中使用thread::timed_join() ,如果线程在给定时间内没有完成,则返回false

Killing the thread is not feasible if your third-party library is not aware of boost:threads. 如果您的第三方库不知道boost:threads,则杀死该线程是不可行的。 Also, you almost certainly don't want to 'kill' the thread without giving the function the possibility to clean up. 而且,你几乎肯定不想在不给功能清理的情况下“杀死”线程。

I'd suggest that you wait for, say, 2 seconds and then continue with some kind of error message, letting the framework function finish its work and just ignoring the result if it came too late. 我建议你等待,比如2秒,然后继续发出某种错误信息,让框架功能完成它的工作,如果来不及就忽略结果。

As for returning a value, I'd suggest something like 至于返回一个值,我建议像

struct myfunction {
   MyObj returnValue;
   void operator() () { 
     // ... 
     returnValue = theComputedReturnValue;
   }
};

// ...
myfunction f;
boost::thread t = boost::thread(boost::ref(f));
t.join(); // or t.timed_join()...
use(f.returnValue); 
// ...

I have done something similar by the past and that works (even though not ideal). 我过去做过类似的事情并且有效(尽管不理想)。 To get the return value just "share" a variable (that could be just a pointer (initially nil) to the returned value, or a full object with a state etc ...) and make your thread read/udate it. 要获得返回值只是“共享”一个变量(可能只是一个指针(最初为零)返回值,或者一个完整的对象具有状态等...)并让你的线程读取/更新它。 Don't forget to mutex it needed. 不要忘记它需要互斥。 That should be quite straight forward. 这应该是非常直截了当的。

Expanding what James has said above, "kill a thread" is such a harsh term! 扩展詹姆斯上面所说的“杀死一个线程”是一个如此严厉的术语! :) But interruption is not so easy either, typically with boost threads, there needs to be an interruption point, where the running thread can be interrupted. :)但是中断也不是那么容易,通常使用boost线程,需要有一个中断点,在这里可以中断正在运行的线程。 There is a set of these interruptible functions (unfortunately they are boost specific), such as wait/sleep etc. One option you have is in the first thread, liberally scatter interruption_points(). 有一组这些可中断函数(不幸的是它们是特定于boost的),例如等待/睡眠等。你有一个选项是在第一个线程中,自由地分散interruption_points()。 Such that when you call interrupt() once thread 2 dies, at the next interruption_point() thread 1 will throw an exception. 这样,当线程2死亡时调用interrupt(),在下一个interruption_point()线程1将抛出异常。

Threads are in the same process space, thus you can have shared state between multiple threads as long as there is synchronized access to that shared state. 线程位于相同的进程空间中,因此只要存在对该共享状态的同步访问,您就可以在多个线程之间拥有共享状态。

EDIT: just noticed that the OP has already looked into this... will leave the answer up anyway I guess... 编辑:刚刚注意到OP已经调查了这个......无论如何我都会留下答案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM