Francis Banyikwa
Tasks
Asynchronous programming in Qt/C++ using tasks and continuations.
The project seeks to do async based programming in Qt/C++ using modern C++.
This library wraps a function into a future where the result of the wrapped function can be retrieved through the future's below public methods:
-
.get()
. This method runs the wrapped function on the current thread and could block the thread and hang GUI. This API is useful when you are already in a background thread. -
then()
. This method does three things: - Registers a method to be called when a wrapped function finish running.
- Runs the wrapped function on a background thread.
- Runs the registered method on the current thread when the wrapped function finish running.
-
.await()
. This method does three things: - Suspends the current thread at a point where this method is called.
- Creates a background thread and then runs the wrapped function in the background thread.
- Unsuspends the current thread when the wrapped function finish and let the current thread continue normally. The suspension at step 1 is done without blocking the thread and hence the suspension can be done in the GUI thread and the GUI will remain responsive.
-
.queue()
. This method runs tasks in a future sequentially and a passed in function will be called when all tasks finish running. This method behaves like.then( [](){} )
if the future is managing only one task. -
.cancel()
. This method can be used to cancel a future. It is important to know that this method does not terminate a running thread that is powering a future, it just releases memory used by a future and this method should be used if a future is to be discarded after it it is acquired but never used. To terminate a thread, call.all_threads()
method, locate a QThread instance you want to terminate and call .terminate() method on the instance. -
.all_threads()
. This method returns a vector of QThreads that are powering futures. The vector will contain a single entry if this future powers its own task. If this future manages other futures, then the returned vector will contain QThread pointers that are in the same order as tasks/futures passed toTask::run()
. -
.start()
. This method is to be used if a future is to be run without caring about its result. Use this API if you want a future to run but dont want to use any of the above mentioned methods. -
.manages_multiple_futures()
. This method can be used to check if a future powers its own task or manages other futures.
Tasks Home page