-
Notifications
You must be signed in to change notification settings - Fork 0
Tasks v0.3.0
fwk::EventTask
and fwk::HttpTask
were removed from the framework (they were only used internally anyway). The API to use both fwk::EventHandle
and fwk::HttpRequest
remains unchanged.
To create fwk::SimpleTask
and fwk::PeriodicTask
you must give a key as first parameter. It is of type const void *
, and can be set to nullptr
if you don't need it.
This key parameter will be useful to purge the task queue of tasks you wan't to delete before they are executed (for example, if the task captured an object that is being destroyed, you can purge the task queue to avoid accessing an invalid memory address).
Creating a fwk::SimpleTask
is the same as before, with a key as first parameter.
fwk::SimpleTask* task = fwk::SimpleTask::getFromPool(
this, // the key, it can be a pointer to anything
[] (void) -> void {
// callback
},
[] (void) -> void {
// cleanup
});
fwk::WorkerManager::get().addTask(task);
// or
fwk::WorkerManager::get().addSimpleTask(
this,
[] (void) -> void {
// callback
},
[] (void) -> void {
// cleanup
});
Same thing for the fwk::PeriodicTask
.
fwk::PeriodicTask* periodicTask = fwk::PeriodicTask::getFromPool();
periodicTask->init(
this, // the key, it can be a pointer to anything
[&] (void) -> void {
// stop executing the task at regular intervals.
periodicTask->stop();
},
[] (void) -> void {
// cleanup
},
std::chrono::seconds(5), // execute the callback every 5 seconds
true); // start executing now (false = 1st execution in 5 seconds)
fwk::WorkerManager::get().addTask(periodicTask);
The cleanup callback of a fwk::SimpleTask
will always be called, after the execution callback, and if the framework is shut down before the task is executed.
With this version, it is now possible to purge the task queue of any task that was added with a specific key (a const void* key
). This key can be anything. If a nullptr
key is given, you cannot purge the task queue.
In this example, we create a fwk::PeriodicTask
with this
as key. The task will be executed the first time in 5 seconds.
Then we purge the task queue, meaning that the fwk::PeriodicTask
will never be executed.
If you wan't to cancel the task, just use periodicTask->stop()
, otherwise the cleanup callback will not be called.
Keep in mind that if you purge the task queue, the cleanup callback given to either fwk::SimpleTask
or fwk::PeriodicTask
will not be called. Be careful with this if you wan't to avoid leaks.
We create the task:
fwk::PeriodicTask* periodicTask = fwk::PeriodicTask::getFromPool();
periodicTask->init(
this, // the key, it can be a pointer to anything
[&] (void) -> void {
// stop executing the task at regular intervals.
periodicTask->stop();
},
[] (void) -> void {
// cleanup
},
std::chrono::seconds(5), // execute the callback every 5 seconds
false); // first execution in 5 seconds
fwk::WorkerManager::get().addTask(periodicTask);
We purge the task queue (asserting that we are in the same object and that the this
key is the same).
fwk::WorkerManager::get().purgeTaskQueue(this);
// now the task is removed from the queue.