classPiiAsyncCall
#include <PiiAsyncCall.h>
A utility class for calling functions asynchronously.
Inherits QThread
Description
PiiAsyncCall is a thread that invokes a member function of a class from a new thread. It can be used to conveniently throw long-lasting processes into threads without deriving from QThread. This class is especially useful if you need to implement many threaded functions in a single class or want to add threaded processing to an object that already derives from QObject.
This class is not intended to be used directly. Use the Pii::asyncCall() and Pii::createAsyncCall() functions to conveniently call functions asynchronously.
Constructors and destructor
|
(
Create a new asynchronous call thread that calls
|
Public member functions
|
void
|
( )
|
|
template< class Object, class Function > QThread *
|
(
Calls a function asynchronously from another thread. |
|
template< class Object, class Function > QThread *
|
(
Creates a thread that will call a function asynchronously. |
Protected member functions
|
(
Create a new asynchronous call thread that calls
|
Function details
-
PiiAsyncCall
(- Object obj
- Function function
[inline, protected]Create a new asynchronous call thread that calls
functiononobject.You probably won't ever use this constructor directly. Use the Pii::asyncCall() helper function instead.
Parameters
- obj
-
the object whose member function is to be called
- function
-
void setAutoDelete
()[inline] -
template< class Object, class Function > QThread * asyncCall
(- Object object
- Function function
- ...
Calls a function asynchronously from another thread.
struct MyStruct { void func() { std::cout << "Moi!" << std::endl; } void func2(const char* message) { std::cout << message << std::endl; } }; MyStruct s; Pii::asyncCall(&s, &MyStruct::func); Pii::asyncCall(&s, &MyStruct::func2, message); // Must have event loop in this thread
Make sure the object pointer is valid in the context of the new thread. Do not pass a pointer to an object on the stack of the calling thread unless you ensure that the async call finishes during the lifetime of the object.
All pararameters passed to the asynchronous function must be copyable.
PiiAsyncCall can call private functions. If you are allowed to take the pointer of a virtual function (i.e. you are in the class itself or in a friend class/function) PiiAsyncCall will be able to use it.
Returns
a pointer to a new QThread object. The thread will be automatically deleted once it is finished and control returns to the event loop of the calling thread.
-
template< class Object, class Function > QThread * createAsyncCall
(- Object object
- Function function
- ...
Creates a thread that will call a function asynchronously.
struct MyStruct { void func() { std::cout << "Moi!" << std::endl; } }; MyStruct s; QThread *pThread = Pii::createAsyncCall(&s, &MyStruct::func); // No automatic delete -> must control the thread myself pThread->start(); pThread->wait(); delete pThread;
Add a note
Not a single note added yet. Be the first, add yours.