ArmarX Threads

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.

namespace armarx
{
class RunningTaskExample :
virtual public Component
{
public:
virtual std::string getDefaultName()
{
return "RunningTaskExample";
}
virtual void onInitComponent()
{
}
virtual void onConnectComponent()
{
// create a running task which uses exampleThreadMethod in this class as thread method
// namespace needs to be specified with the method name
exampleTask = new RunningTask<RunningTaskExample>(this, &RunningTaskExample::exampleThreadMethod);
// start the thread
exampleTask->start();
}
virtual void onDisconnectComponent()
{
// stop the thread and wait for join
exampleTask->stop();
}
void exampleThreadMethod()
{
// do the work
printf("Thread method\n");
// shutdown is set if exampleTask->stop() is called.
// running tasks need to handle the shutdown by themselves.
// Here it is essentially not necessary but just serves as an example
while(!exampleTask->isStopped())
{
usleep(10);
// if the sleep time should be synchronized with Simulator time,
// use TimeUtil::Sleep(IceUtil::Time) or TimeUtil::MSSleep(int) instead
// Especially this will pause execution when the simulator is paused
//TimeUtil::MSSleep(1)
}
}
private:
// shared pointer to RunningTask
};
}

Using periodic tasks

The periodic task executes one thread method repeatedly using the time period specified in the constructor.

namespace armarx
{
class PeriodicTaskExample :
virtual public Component
{
public:
virtual std::string getDefaultName()
{
return "PeriodicTaskExample";
}
virtual void onInitComponent()
{
}
virtual void onConnectComponent()
{
// create a running task which uses exampleThreadMethod in this class as thread method
// namespace needs to be specified with the method name
// will be called every second
exampleTask = new PeriodicTask<PeriodicTaskExample>(this, &PeriodicTaskExample::exampleThreadMethod, 1000);
// start the thread
exampleTask->start();
}
virtual void onDisconnectComponent()
{
// stop the thread and wait for join
exampleTask->stop();
}
void exampleThreadMethod()
{
// do the work
printf("Thread method\n");
}
private:
// shared pointer to PeriodicTask
};
}
PeriodicTask.h
RunningTask.h
armarx::PeriodicTask::pointer_type
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
Definition: PeriodicTask.h:67
Component.h
armarx::RunningTask::pointer_type
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
Definition: RunningTask.h:178
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28