4

Normal QThread can be terminated using terminate() Slot, thread will terminates based on platform which thread running and based on OS scheduling settings/policies. I know it is not wise to terminate a thread.

My questine is can anyone know is there any similar API for QtConcurrent. Since I started another thread using QtConcurrent API, called map. So I need to abort/cancel the running thread from callstack(only single thread I use). Do we have any API/method for this?

Note: QFutureWatcher having pause/resume/cancel but it seems these are used for threadpool usage context( I mean pause/cancel/resume thread collections), not for individual thread/call stack context.

Ashif
  • 1,652
  • 14
  • 30
  • 1
    You may add exit logic inside your threaded function. – Dmitry Sazonov Jan 18 '18 at 12:34
  • OK, that means is it not possible to terminate a thread started with QtConcurrent API from another thread ?. – Ashif Jan 18 '18 at 12:40
  • 1
    It is possible, but unsafe. And requre a complex code. – Dmitry Sazonov Jan 18 '18 at 12:57
  • @Ashif, .. I think the `QtConcurrent::Run` is designed for efficient _small_ threads that are supposed to execute fast .. this could be the reason the cancel effort complexity is left out! that said, `QtConcurrent::mappedReduced()` can be canceled. – Mohammad Kanan Jan 19 '18 at 11:20
  • cancel on QtConcurrent::mappedReduced() API doesn't cancel the current running thread, it cancel only the thread pool workers tasks. i.e if we have 10 workers/thread planned to execute 100 tasks, if 50 tasks completed, and user perform "cancel", remaining tasks ignored. It does not mean a worker can cancel his work in the middle and exit the "run" loop. – Ashif Jan 19 '18 at 11:51
  • @Dmitry Sazonov; Do you mean the worker thread need to check abort status whenever necessary and return accordingly?. If that, we can have two approaches a)global variable. b) Qt::Directconnection with master and worker thread, so that abort flag can be updated from master thread, while worker thread is busy in executing. – Ashif Jan 19 '18 at 11:57
  • a) Yes, this is what i wrote above. b) You are totally wrong. You really need to read Qt documentation about [conection types](http://doc.qt.io/qt-5/qt.html#ConnectionType-enum) and about [signals and slots](http://doc.qt.io/qt-5/signalsandslots.html). Brief: you need a running event loop to call slots in another threads. But you don't. – Dmitry Sazonov Jan 19 '18 at 12:08
  • b) I have GUI thread/master which connects(Qt::DirectConnection) with worker thread, whenever abort button is clicked slot of the worker thread will be executed, the thread id of the slot of worker thread is GUI thread. Is that possible right ? – Ashif Jan 19 '18 at 12:26
  • Yes, `Qt::DirectConnection` will execute a slot in same thread, where you call `emit`. It's like a direct method call. But you should use atomic operations for checking and changing your flag. And you should be sure that object exists and not killed in another thread. – Dmitry Sazonov Jan 20 '18 at 08:47
  • terminating a thread is not encouraged, instead let the thread exit itself when time comes. I think, it is kind of analogy of human life, let the time comes to exit with whatever condition it would be. termination is a crime, there should be some stain/memory held, which makes the further life is complex. Thank you for updating the insights on this. – Ashif Jan 22 '18 at 05:06

0 Answers0