cumbia 1.x
general purpose multi threaded library
|
cumbia thread implementation More...
#include <cuthread.h>
Public Types | |
enum | Type { CuThreadType = 0 } |
Public Member Functions | |
CuThread (const std::string &token, CuThreadsEventBridge_I *threadEventsBridge, const CuServiceProvider *sp, std::vector< CuThreadInterface * > *thv_p) | |
builds a new CuThread More... | |
virtual | ~CuThread () |
the class destructor, deletes the thread and the event bridge More... | |
void | registerActivity (CuActivity *l, CuThreadListener *tl) |
Register a new activity on this thread. More... | |
void | unregisterActivity (CuActivity *l) |
unregister the activity passed as argument from this thread. More... | |
void | publishProgress (const CuActivity *activity, int step, int total, const CuData &data) |
invoked in CuThread's thread, posts a progress event to the main thread More... | |
void | publishResult (const CuActivity *activity, const CuData &data) |
invoked in CuThread's thread, posts a result event to main thread More... | |
void | publishResult (const CuActivity *activity, const std::vector< CuData > &data_list) |
post a result event from the background thread to the main thread More... | |
void | publishResult (const CuActivity *activity, const CuUserData *u) |
post a custom user event data to the specified activity More... | |
bool | matches (const std::string &other_thtok) const |
returns true if this thread token is equal to other_thread_token More... | |
std::string | getToken () const |
returns the thread token that was specified at construction time More... | |
void | cleanup () |
int | type () const |
returns 0 More... | |
void | start () |
internally used, allocates a new std::thread More... | |
bool | isRunning () |
returns true if the thread is running More... | |
void | onTimeout (CuTimer *sender) |
void | wait () |
wait for the thread to exit More... | |
void | exit () |
exit the thread loop gracefully More... | |
void | postEvent (CuActivity *a, CuActivityEvent *e) |
sends the event e to the activity a from the main thread to the background More... | |
void | onEventPosted (CuEventI *event) |
implements onEventPosted from CuThreadsEventBridgeListener interface. Invokes onProgress or onResult on the registered CuThreadListener objects. More... | |
Public Member Functions inherited from CuThreadInterface | |
virtual | ~CuThreadInterface () |
interface virtual destructor More... | |
virtual void | registerActivity (CuActivity *l, CuThreadListener *tl)=0 |
register and start a new activity More... | |
virtual void | unregisterActivity (CuActivity *l)=0 |
unregister and, if required, automatically delete an activity More... | |
virtual void | publishProgress (const CuActivity *activity, int step, int total, const CuData &d)=0 |
post a progress event from the background thread to the main thread More... | |
virtual void | publishResult (const CuActivity *activity, const CuData &d)=0 |
post a result event from the background thread to the main thread More... | |
virtual void | publishResult (const CuActivity *activity, const std::vector< CuData > &data_list)=0 |
post a result event from the background thread to the main thread More... | |
virtual void | publishResult (const CuActivity *activity, const CuUserData *u)=0 |
post a custom user event data to the specified activity More... | |
virtual bool | matches (const std::string &other_thread_token) const =0 |
Used by the thread factory, this function, given an input data, determines whether this thread is ecuivalent to another thread of the same class for the data. More... | |
virtual void | cleanup ()=0 |
perform clean operations on the class members More... | |
virtual int | type () const =0 |
return an integer to allow rtti More... | |
virtual void | start ()=0 |
method to start the thread in the background More... | |
virtual bool | isRunning ()=0 |
returns true if the thread is running, false otherwise More... | |
virtual void | wait ()=0 |
wait for the thread to exit More... | |
virtual void | exit ()=0 |
gracefully exit the thread More... | |
virtual void | postEvent (CuActivity *, CuActivityEvent *)=0 |
forward an event to an activity More... | |
virtual void | onEventPosted (CuEventI *event)=0 |
onEventPosted from CuThreadsEventBridgeListener interface. More... | |
Public Member Functions inherited from CuThreadsEventBridgeListener | |
virtual | ~CuThreadsEventBridgeListener () |
virtual void | onEventPosted (CuEventI *e)=0 |
callback on the main thread containing an event proceeding from the background thread More... | |
Protected Member Functions | |
virtual void | run () |
cumbia thread implementation
CuThread offers a C++11 thread implementation that can be used with cumbia. Threads in cumbia applications are usually instantiated and managed by the CuThreadService cumbia service. Please read the introduction to CuThreadService documentation to understand how cumbia deals with them in an application (cuthread_service).
enum CuThread::Type |
CuThread::CuThread | ( | const std::string & | token, |
CuThreadsEventBridge_I * | teb, | ||
const CuServiceProvider * | sp, | ||
std::vector< CuThreadInterface * > * | thv_p | ||
) |
builds a new CuThread
thread_token | the token associated to the thread |
eventsBridge | a CuThreadsEventBridge_I implementation, for example CuThreadsEventBridge (see CuThreadsEventBridgeFactory::createEventBridge) or QThreadsEventBridge, recommended for Qt applications (see QThreadsEventBridgeFactory::createEventBridge) |
service_provider | the CuServiceProvider of the application |
|
virtual |
|
virtual |
does nothing
Implements CuThreadInterface.
|
virtual |
exit the thread loop gracefully
an ExitThreadEvent is queued to the event queue to exit the thread
Implements CuThreadInterface.
std::string CuThread::getToken | ( | ) | const |
returns the thread token that was specified at construction time
|
virtual |
returns true if the thread is running
Implements CuThreadInterface.
|
virtual |
returns true if this thread token is equal to other_thread_token
other_thread_token | a CuData that's the token of another thread |
When Cumbia::registerActivity is used to execute a new activity, the thread token passed as input argument is relevant to decide whether the new registered activity must be run in a running thread (if one is found with the same token) or in a new one (no threads found with the token given to CuActivity::registerActivity).
See also getToken
Implements CuThreadInterface.
|
virtual |
implements onEventPosted from CuThreadsEventBridgeListener interface. Invokes onProgress or onResult on the registered CuThreadListener objects.
This method gets a reference to a CuActivityManager through the service provider.
Implements CuThreadInterface.
References CuEventI::CuA_ExitEvent, CuEventI::CuA_UnregisterEv, CuEventI::CuProgressEv, CuEventI::CuResultEv, CuEventI::CuThAutoDestroyEv, CuResultEvent::data, CuResultEvent::datalist, CuResultEvent::getActivity(), CuResultEvent::getStep(), CuResultEvent::getTotal(), CuResultEvent::getType(), CuResultEvent::isList(), CuResultEvent::u_data, and wait().
void CuThread::onTimeout | ( | CuTimer * | sender | ) |
|
virtual |
sends the event e to the activity a from the main thread to the background
a | the CuActivity that sends the event |
e | the CuActivityEvent |
There can be situations where clients can call the above listed Cumbia methods. On the other hand, CuThread::postEvent is not normally intended for direct use by clients of this library.
Implements CuThreadInterface.
|
virtual |
invoked in CuThread's thread, posts a progress event to the main thread
activity | the addressee of the event |
step | the current step of the progress |
total | the total number of steps making up the whole work |
data | the data to be delivered with the progress event |
CuResultEvent is used in conjunction with CuThreadsEventBridge_I::postEvent
Implements CuThreadInterface.
|
virtual |
invoked in CuThread's thread, posts a result event to main thread
a | the recepient of the event |
data | the data to be delivered with the result |
CuResultEvent is used in conjunction with CuThreadsEventBridge_I::postEvent
Implements CuThreadInterface.
|
virtual |
post a custom user event data to the specified activity
activity | the recipient |
u | a custom CuUserData derived data |
Implements CuThreadInterface.
|
virtual |
post a result event from the background thread to the main thread
This lets you send events to the main thread that account for a list of results computed in the background
See CuThread::publishProgress for details
Implements CuThreadInterface.
|
virtual |
Register a new activity on this thread.
This is invoked from the main thread. As soon as registerActivity is called, a RegisterActivityEvent is added to the thread event queue and the CuActivity will be soon initialised and executed in the background. (CuActivity::init and CuActivity::execute)
l | a CuActivity that is the worker, whose methods are invoked in this background thread. |
Implements CuThreadInterface.
References CuActivity::setThreadToken().
|
protectedvirtual |
Thread loop
References datos, CuActivity::doExecute(), CuActivity::event(), CuTimeoutChangeEvent::getTimeout(), CuActivityEvent::getType(), i, perr, CuActivity::repeat(), CuTimer::timeout(), and CuActivityEvent::TimeoutChange.
Referenced by start().
|
virtual |
internally used, allocates a new std::thread
return a new instance of std::thread
Implements CuThreadInterface.
|
virtual |
|
virtual |
unregister the activity passed as argument from this thread.
l | the CuActivity to unregister from this thread. |
An UnRegisterActivityEvent is queued to the thread event queue. When processed, CuActivity::doOnExit is called which in turn calls CuActivity::onExit (in the CuActivity background thread). If the flag CuActivity::CuADeleteOnExit is true, the activity is later deleted (back in the main thread)
Thread: main
Implements CuThreadInterface.
|
virtual |
wait for the thread to exit
call join on the thread
Implements CuThreadInterface.
Referenced by onEventPosted().