ArmarX provides utility methods in order to include different types of threads.
In order to support the inclusion of multiple threads in a single objects, without the neccessity to write additional thread classes the mechanism uses functionoids of class methods. Two types of threads are provided by the ArmarX core: the RunningTask and the PeriodicTask. These can be extended in projects which use the ArmarX core.
The API documentation can be found here: Threads
Using running tasks
The running task executes one thread method once. The task can be started only once.
{
class RunningTaskExample :
virtual public Component
{
public:
virtual std::string getDefaultName()
{
return "RunningTaskExample";
}
virtual void onInitComponent()
{
}
virtual void onConnectComponent()
{
exampleTask = new RunningTask<RunningTaskExample>(this, &RunningTaskExample::exampleThreadMethod);
exampleTask->start();
}
virtual void onDisconnectComponent()
{
exampleTask->stop();
}
void exampleThreadMethod()
{
printf("Thread method\n");
while(!exampleTask->isStopped())
{
usleep(10);
}
}
private:
RunningTask<RunningTaskExample>::pointer_type exampleTask;
};
}
This file offers overloads of toIce() and fromIce() functions for STL container types.
Using periodic tasks
The periodic task executes one thread method repeatedly using the time period specified in the constructor.
{
class PeriodicTaskExample :
virtual public Component
{
public:
virtual std::string getDefaultName()
{
return "PeriodicTaskExample";
}
virtual void onInitComponent()
{
}
virtual void onConnectComponent()
{
exampleTask = new PeriodicTask<PeriodicTaskExample>(this, &PeriodicTaskExample::exampleThreadMethod, 1000);
exampleTask->start();
}
virtual void onDisconnectComponent()
{
exampleTask->stop();
}
void exampleThreadMethod()
{
printf("Thread method\n");
}
private:
PeriodicTask<PeriodicTaskExample>::pointer_type exampleTask;
};
}