C ++ implements thread pool cancellation

I have several thread pools, and I want my application to handle the cancel operation.

To do this, I implemented a controller object of a shared operating system, which I polled at various places in each calling function of the thread's workflow.

Is this a good model or is there a better way to do this?

I'm just worried that all of these Controller.checkState () operations are clogged in the whole code.

+4
source share
5 answers

Yes, this is a good approach. Herb Sutter has a good article comparing it with alternatives (which is worse).

+4
source

For any asynchronous cancellation, you will have to periodically try some kind of flag. There is a fundamental question about keeping things in agreement. If you just kill a thread in the middle of everything that it does, bad things happen sooner or later.

Depending on what you are actually doing, you can simply ignore the result of the operation, rather than cancel it. You continue the operation, but just do not wait for its completion and never check the result.

If you really need to stop the operation, you will need to interrogate at the appropriate points and perform any cleaning.

+1
source

This is a good way to do it.

Another possible way to do this is if there is some other routine [s] that the threads call regularly to check inside this routine and throw an exception (to be caught at the top of the thread), assuming that "cancel" can be considered exceptional and assuming the code executed by the thread is safe for exceptions.

+1
source

I wouldn’t do this while checking out a shared object.

I will most likely provide each thread object with a way to cancel execution inside its own thread, be it an event, a thread safety status variable, or something else.

The problem with the general controller of operations is that, from my point of view, the logic is canceled, why do you call it a “controller” when it does not control anything?

For me, the operation controller should receive a cancellation order, and then, in turn, select the appropriate flows and warn them about the termination. That would be the right “chain of commands" if you knew what I mean. The way you do this, you introduce unnatural behavior in a stream that does not “obey” orders to stop, and if it checks every time, if its “boss” “wrote the order”. Somehow it just doesn't seem right.

In addition, what if you are only one "some" of the threads to stop in the future? What if you want to enable some advanced logic so that threads stop setting the condition? Then you will have to rewrite the code in each thread to handle this condition.

Therefore, I will provide a way so that each thread can process signals to them, for example, using a Command Pattern with a FIFO structure.

(By the way, I understand that they are employees of the thread pool, and not the actual Thread classes, but still, I think every worker should be signaled in order to stop separately, and not vice versa).

+1
source

In such situations, I used the event, non-auto-reset, all threads can look at this event. It is very similar to polling, except that if your threads are sometimes blocked, they can sleep for a "stop" event. (Easier on Windows.)

/ l

0
source

All Articles