1

I have the same situation like this: stop thread started by qtconcurrent::run

I need to close child thread (started with QtConcurrent::run) on closeEvent in QMainWindow. But my function in child thread use code from *.dll: I can`t use loop because all that I do - is calling the external dll like

QFuture<void> = QtConcurrent::run(obj->useDllfunc_with_longTermJob());

And when I close the app with x-button my gui is closed, but second thread with_longTermJob() still worked and when is finished I have an error. I know some decisions for this:

  1. using other functions like map() or something else with QFuture.cancel/stop functionality, not QtConcurrent::run().But I need only one function call. run() is what I need.
  2. or use QThread instead Concurrent.But it`s not good for me.

What method more simple and better and how can I implement this? Is there a method that I don`t listed? Could you provide small code sample for decision. Thx!

Community
  • 1
  • 1
Meteo ir3
  • 449
  • 8
  • 21
  • http://stackoverflow.com/questions/32952474/non-blocking-worker-interrupt-file-copy?lq=1 – dtech Jun 13 '16 at 20:08

1 Answers1

1

QtConcurrent::run isn't a problem here. You must have means of stopping the dllFuncWithLongTermJob. If you don't have such means, then the API you're using is broken, and you're out of luck. There's nothing you can do that'd be generally safe. Forcibly terminating a thread can leave the heap in an inconsistent state, etc. - if you need to terminate a thread, you need to immediately abort the application.

Hopefully, you can call something like stopLongTermJob that sets some flag that interrupts the dllFuncWithLongTermJob.

Then:

auto obj = new Worker;
auto objFuture = QtConcurrent::run([=]{obj->dllFuncWithLongTermJob();});

To interrupt:

obj->stopLongTermJob(); // must be thread-safe, sets a flag
objFuture.waitForFinished();
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • 1
    Actually there is something the OP might do, but it all depends on what the DLL does. If dllFuncWithLongTermJob is just a stateless run-once-and-forget-about-it kind of function, then it could be run in a child process, which could be killed without serious consequences for the main program. That's just using the OS to do the cleanup work. Needless to say, this is a last resort solution that I would only use for lack of a properly interruptible alternative. – odelande Jun 13 '16 at 22:42
  • @odelande There's way more to the benefit of running it in a separate process. The OS cleanup is one part, certainly. The other important part is that the dll won't corrupt *your* state when it gets abruptly terminated. Running it in a child process is an excellent idea, in fact! – Kuba hasn't forgotten Monica Jun 14 '16 at 13:07
  • If I understood correctly - I need to use QThread instead QtConcurrent for control child thread? Concurrent doesn`t useful in this situation? At that moment I just implement closeEvent in MainWindow for waiting dialog if child threads count != 0x00. – Meteo ir3 Jun 16 '16 at 15:38