PDA

View Full Version : QtConcurrent::run() and exit()



pmitas
23rd September 2010, 22:50
Long story short: when I call exit() from a process started from QtConcurrent::run() it enters an infinite loop here:
0 pthread_cond_wait pthread_cond_wait.S 162 0x00007ffff609459c
1 wait qwaitcondition_unix.cpp 87 0x00007ffff6a27a7b
2 QWaitCondition::wait qwaitcondition_unix.cpp 159 0x00007ffff6a27a7b
3 QThreadPoolPrivate::waitForDone qthreadpool.cpp 295 0x00007ffff6a1c808
4 QThreadPool::~QThreadPool qthreadpool.cpp 428 0x00007ffff6a1cfff
5 QGlobalStaticDeleter<QThreadPool>::~QGlobalStaticDeleter qglobal.h 1796 0x00007ffff6a1d695
6 __run_exit_handlers exit.c 78 0x00007ffff55ad045
7 exit exit.c 100 0x00007ffff55ad095
8 fsElement::fsElement fselement.cpp 41 0x000000000040b266
9 fsElement::populateDirList fselement.cpp 65 0x000000000040b70b
10 fsElement::findFilesAndDirs fselement.cpp 120 0x000000000040be77
11 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
12 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
13 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
14 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
15 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
16 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
17 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
18 fsElement::findFilesAndDirs fselement.cpp 122 0x000000000040be9c
19 QtConcurrent::VoidStoredMemberFunctionPointerCall0<void, fsElement>::runFunctor qtconcurrentstoredfunctioncall.h 216 0x000000000040adc4
20 QtConcurrent::RunFunctionTask<void>::run qtconcurrentrunbase.h 120 0x000000000040a396
21 QThreadPoolThread::run qthreadpool.cpp 106 0x00007ffff6a1ca4f
22 QThreadPrivate::start qthread_unix.cpp 248 0x00007ffff6a26a25
23 start_thread pthread_create.c 301 0x00007ffff608fc1a
24 clone clone.S 115 0x00007ffff5649a9d
So I guess I shouldn't use exit() in this fashion. Is there some other way to exit a thread in this situation?

Bonus, longer question: I just want a way to perform some heavy i/o work and still keep the GUI up to date and responsive (I'm writing a file searching app as an exercise). Doing it in a seperate thread worked quite well, but I'd also like to have the ability to pause and cancel the work. Is threading the best way to do it? What should I do to do it right? QThread? QRunnable? Or is QtConcurrent the best way?

wysota
23rd September 2010, 23:42
Keeping the GUI Responsive

pmitas
24th September 2010, 00:56
Great article, thanks. But what about my first question? Is there no way to exit a process in a similar way?

wysota
24th September 2010, 03:28
You want to exit the whole process or just the thread?

pmitas
24th September 2010, 09:04
Oops. Yeah, whenever I said "process" I meant "thread" :P

wysota
24th September 2010, 09:25
Just return from your concurrent function then. You cannot abort a running concurrent function from outside - it could render your application in an unpredictable state. If you want to be able to abort an inprogress operation, you have to use some other approach (i.e. the "solving the problem step by step" one from the article - either in an external thread or in the main thread).

pmitas
24th September 2010, 09:42
Just return from your concurrent function then. It's a multi-branch recursive function, returning to the bottom wouldn't be easy.

You cannot abort a running concurrent function from outside - it could render your application in an unpredictable state. I'm not - I'm trying to abort it from the inside at a safe point in response to a flag being set.

To sum things up, I just want to be able to call a function from a thread that would make the thread just drop everything and stop messing with my data structures:) Can I do that?

wysota
24th September 2010, 09:53
You can throw exceptions from your function and catch them at the upper-most level and then return from the thread function.

pmitas
24th September 2010, 10:12
Good suggestion. Thanks again.