#include <wx/thread.h>
The wxThreadHelper class is a mix-in class that manages a single background thread, either detached or joinable (see wxThread for the differences).
By deriving from wxThreadHelper, a class can implement the thread code in its own wxThreadHelper::Entry() method and easily share data and synchronization objects between the main thread and the worker thread.
Doing this prevents the awkward passing of pointers that is needed when the original object in the main thread needs to synchronize with its worker thread in its own wxThread derived object.
For example, wxFrame may need to make some calculations in a background thread and then display the results of those calculations in the main window.
Ordinarily, a wxThread derived object would be created with the calculation code implemented in wxThread::Entry. To access the inputs to the calculation, the frame object would often need to pass a pointer to itself to the thread object. Similarly, the frame object would hold a pointer to the thread object.
Shared data and synchronization objects could be stored in either object though the object without the data would have to access the data through a pointer. However with wxThreadHelper the frame object and the thread object are treated as the same object. Shared data and synchronization variables are stored in the single object, eliminating a layer of indirection and the associated pointers.
Example:
Public Member Functions | |
wxThreadHelper (wxThreadKind kind=wxTHREAD_JOINABLE) | |
This constructor simply initializes internal member variables and tells wxThreadHelper which type the thread internally managed should be. | |
virtual | ~wxThreadHelper () |
The destructor frees the resources associated with the thread, forcing it to terminate (it uses wxThread::Kill function). | |
virtual ExitCode | Entry ()=0 |
This is the entry point of the thread. | |
virtual void | OnDelete () |
Callback called by Delete() before actually deleting the thread. | |
virtual void | OnKill () |
Callback called by Kill() before actually killing the thread. | |
wxThreadError | Create (unsigned int stackSize=0) |
wxThreadError | CreateThread (wxThreadKind kind=wxTHREAD_JOINABLE, unsigned int stackSize=0) |
Creates a new thread of the given kind. | |
wxThread * | GetThread () const |
This is a public function that returns the wxThread object associated with the thread. | |
wxThreadKind | GetThreadKind () const |
Returns the last type of thread given to the CreateThread() function or to the constructor. | |
wxThreadHelper::wxThreadHelper | ( | wxThreadKind | kind = wxTHREAD_JOINABLE | ) |
This constructor simply initializes internal member variables and tells wxThreadHelper which type the thread internally managed should be.
|
virtual |
The destructor frees the resources associated with the thread, forcing it to terminate (it uses wxThread::Kill function).
Because of the wxThread::Kill unsafety, you should always wait (with wxThread::Wait) for joinable threads to end or call wxThread::Delete on detached threads, instead of relying on this destructor for stopping the thread.
wxThreadError wxThreadHelper::Create | ( | unsigned int | stackSize = 0 | ) |
wxThreadError wxThreadHelper::CreateThread | ( | wxThreadKind | kind = wxTHREAD_JOINABLE , |
unsigned int | stackSize = 0 |
||
) |
Creates a new thread of the given kind.
The thread object is created in the suspended state, and you should call GetThread()->Run() to start running it.
You may optionally specify the stack size to be allocated to it (ignored on platforms that don't support setting it explicitly, e.g. Unix).
|
pure virtual |
This is the entry point of the thread.
This function is pure virtual and must be implemented by any derived class. The thread execution will start here.
You'll typically want your Entry() to look like:
The returned value is the thread exit code which is only useful for joinable threads and is the value returned by "GetThread()->Wait()"
.
This function is called by wxWidgets itself and should never be called directly.
wxThread* wxThreadHelper::GetThread | ( | ) | const |
This is a public function that returns the wxThread object associated with the thread.
wxThreadKind wxThreadHelper::GetThreadKind | ( | ) | const |
Returns the last type of thread given to the CreateThread() function or to the constructor.
|
virtual |
Callback called by Delete() before actually deleting the thread.
This function can be overridden by the derived class to perform some specific task when the thread is gracefully destroyed. Notice that it will be executed in the context of the thread that called Delete() and not in this thread's context.
TestDestroy() will be true for the thread before OnDelete() gets executed.
|
virtual |
Callback called by Kill() before actually killing the thread.
This function can be overridden by the derived class to perform some specific task when the thread is terminated. Notice that it will be executed in the context of the thread that called Kill() and not in this thread's context.