-
Notifications
You must be signed in to change notification settings - Fork 1
Tasks
The VxWorks task library is supported by vwpp through an abstract class called VWPP::Task. To create task objects, derive a class from VWPP::Task and define the virtual method taskEntry(). The derived class can hold as much state as it needs through the use of class members.
For instance, a class that simply prints "Hello, World!" would be defined as this:
class HelloTask : public VWPP::Task {
public:
void taskEntry() { puts("Hello, World!"); }
};The run() method starts the task with specified startup parameters. In this case, the task name is "tHello" running at priority level 100 using a stack size of 1024 bytes. A simple program that spawns the HelloTask at 1Hz could be defined as[^1]:
while (1) {
HelloTask task;
task.run("tHello", 100, 1024);
taskDelay(60);
}NOTE: Starting with v2.7, we introduced a nested, version namespace to enforce matching APIs when building and deploying. In the following code examples, we refer to the namespace as "VWPP". This translates to vwpp for pre-2.7 libraries and to vwpp::v2_7 for v2.7.
VWPP::Task()The constructor allows the Task object to initialize its state. To actually start the thread, use VWPP::Task::run().
void VWPP::Task::delay(int ms) constStops the task from running for the given number of milliseconds. This method can only be called from within the context of a derived class.
bool VWPP::Task::isSuspended() constReturns true if the task is suspended. This method is only useful, naturally, when called by another task to see if the task is suspended.
char const* VWPP::Task::name() constReturns the given name of the task.
int VWPP::Task::priority() constThis method returns the priority of the task.
VWPP::Task::run(char const* name, uint8_t priority, int stackSize)Spawns a task with the provided name. The task runs with the given priority. Its stack is sized based upon the provided stackSize. If the task was successfully created, it will immediately start running its taskEntry() method. If the task couldn't be created, std::runtime_error is thrown. The taskEntry() method is run from within a try-catch block that catches all exceptions, preventing unhandled exceptions from terminating the task. If this outer catch block catches an exception, it suspends the task. Resuming the task will let it clean up gracefully.
void VWPP::Task::taskEntry()This method is overridden by derived classes to do the task's actual work. If this function returns, the task will exit.
void VWPP::Task::yieldCpu() constLets any other runnable tasks at the same priority have CPU time. This method can only be called from within the context of a derived class.
[^1]: This example works because HelloTask completes its job much quicker than the delay time. If it took longer than 1 second to complete, it would get destroyed by the destructor that gets called at the end of the while block.