PDA

View Full Version : Making library non-blocking



doggrant
23rd February 2010, 16:08
Hi,

I have a library which contains blocking function calls, which I can't change. Therefore i need to make them non-blocking. I was thinking about using QFuture and QFutureWatcher to do this, since I can pass in the library parameter when creating the furture, and wait on for the finished () signal from the QFutureWatcher to get the result back from the future.

My problem is, i'm not sure where and how i should be destorying the QFuture and QFutureWatcher, since I would create them when making the call to the library, but do not know how to get any references to them when the finished() signal is emitted.

Any ideas, or will they be destroyed automatically when the finished signal is fired.

spud
23rd February 2010, 16:39
The QFutureWatcher will not be deleted, but you could call

sender()->deleteLater();
or just keep it as a member variable, then you won't have to delete it at all.
No need to worry about the QFuture since it isn't allocated on the heap

doggrant
23rd February 2010, 17:48
Thanks for that. I'm now not sure whether QFuture is the way to go, or whether I should just have a class which is QRunnable, and then passing an instance to the global QThreadPool when I need to execute it. Then I can just use signals and slots to pass data between the GUI thread and the threads i am using to make the 3rd party library non-blocking.

I was looking at QFuture, because the docs mentioned about them, and they seemed useful. Do you agree on my ideas above?

spud
23rd February 2010, 18:18
I haven't used QRunnable directly my self so I can't really judge, but I think that QtConcurrent::run() in combination with a QFutureWatcher is a perfect fit for this kind of "single shot" concurrency.
It is my impression that going with QRunnable/QThreadPool would only pay off when you have a number of concurrent operations that you want to keep track of.

wysota
23rd February 2010, 20:46
Watch out not to run out of threads in the thread pool if you wish to execute more than one call from the library at once. Also first make sure the functions in the library are thread-safe or at least reentrant. Otherwise things may blow up in your face.