简体   繁体   English

c ++跨线程池实现取消

[英]c++ implementing cancel across thread pools

I have several thread pools and I want my application to handle a cancel operation. 我有几个线程池,我希望我的应用程序处理取消操作。

To do this I implemented a shared operation controller object which I poll at various spots in each thread pool worker function that is called. 为此,我实现了一个共享操作控制器对象,我在每个被调用的线程池工作器函数中的各个位置进行轮询。

Is this a good model, or is there a better way to do it? 这是一个很好的模型,还是有更好的方法呢?

I just worry about having all of these operationController.checkState() littered throughout the code. 我只是担心在整个代码中散布所有这些operationController.checkState()。

Yes it's a good approach. 是的,这是一个很好的方法。 Herb Sutter has a nice article comparing it with the alternatives (which are worse). Herb Sutter有一篇很好的文章将它与替代品(更糟糕的)相比较。

With any kind of ansynchronous cancellation you're going to have to periodically poll some sort of flag. 通过任何类型的异步取消,您将不得不定期轮询某种标志。 There's a fundamental issue of having to keep things in a consitant state. 必须将事物保持在一个共同状态是一个根本问题。 If you just kill a thread in the middle of whatever it's doing, bad things will happen sooner or later. 如果你只是在它正在做的事情中间杀死一个线程,迟早会发生坏事。

Depending on what you are actually doing, you may be able to just ignore the result of the operation instead of cancelling it. 根据您实际执行的操作,您可以忽略操作的结果而不是取消它。 You let the operation continue on, but just don't wait for it to complete and never check the result. 您让操作继续,但不要等待它完成,不要检查结果。

If you actually need to stop the operation, then you're going to have to poll at appropriate points, and do whatever cleanup is necessary. 如果你真的需要停止操作,那么你将不得不在适当的点进行轮询,并做必要的清理工作。

It's a good way to do it. 这是一个很好的方法。

Another possible way to do it is, if there's some other subroutine[s] which the threads call regularly anyway, to check within that subroutine and throw an exception (to be caught at the top of the thread), assuming that "cancel" may be considered exceptional and assuming that the code being executed by the thread is exception-safe. 另一种可能的方法是,如果有一些其他子程序[s],线程仍然会定期调用,检查该子程序并抛出异常(被捕获在线程的顶部),假设“取消”可能被认为是例外,并假设线程执行的代码是异常安全的。

I wouldn't do it that way, checking a shared object. 我不会这样做,检查共享对象。

I most likely will provide each thread object with a way to cancel the execution inside the own thread, be it an event, a threadsafe state variable or whatever. 我很可能会为每个线程对象提供一种取消自己线程内执行的方法,无论是事件,线程安全状态变量还是其他什么。

The problem with the shared operation controller is that, from my point of view, the logic is reversed, Why are you calling it "controller" when it doesn't control anything? 共享操作控制器的问题在于,从我的观点来看,逻辑是相反的,当它不控制任何东西时,为什么称它为“控制器”?

For me, Operation Controller shall recive a cancelation order and then, in turn select the appropiate threads and signal them to stop. 对我来说,操作控制器应该重新获得取消订单,然后选择适当的线程并发出信号停止。 That would be a correct "chain of command" if you know what I mean. 如果你知道我的意思,那将是一个正确的“指挥链”。 The way you do it you introduce an unnatural behaivour on the thread wich doesn't "obey" orders to stop, instead if checks each time if his "superior" has "written the order somewere". 你这样做的方法就是在线程中引入一种不自然的行为,而不是“服从”命令停止,而是每次检查他的“上司”是否“写了一些订单”。 Somehow it just doesn't feel right. 不知何故,它感觉不对劲。

In addition, what if you just one "some" of the threads to stop in the future? 另外,如果你将来只停留一些“部分”线程怎么办? What if you want to include some advanced logic so that threads will only stop given a condition? 如果你想包含一些高级逻辑以便线程只在条件下停止怎么办? Then you'll have to rewrite the code in each and every thread to handle that condition. 然后你将不得不重写每个线程中的代码来处理这个条件。

So I will provide a way, for each thread to be able to handle signals to them, for example by using a Command Pattern with a FIFO structure. 因此,我将为每个线程提供一种方法,以便能够处理信号,例如使用具有FIFO结构的命令模式

(By the way, I realize they're thread pool workers, not actual Thread Classes but still, I think each worker must be signaled to stop separately, not the other way around). (顺便说一句,我意识到他们是线程池工作者,而不是实际的线程类,但我认为每个工作者必须被发信号分别停止,而不是相反)。

In similar situations I have used an event, non-auto-reset, all threads can look at that event. 在类似的情况下,我使用了一个事件,非自动重置,所有线程都可以查看该事件。 Quite similar to polling except that if your threads block at times, they can sleep for the "stop"-event as well. 与轮询非常相似,除非你的线程有时会阻塞,它们也可以为“停止”事件而休眠。 (Easier on Windows.) (在Windows上更容易。)

/L / L

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

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