template class Poco::Activity

Overview

This template class helps to implement active objects. Moreā€¦

#include <Activity.h>

template <class C>
class Activity: public Poco::Runnable
{
public:
    // typedefs

    typedef RunnableAdapter<C> RunnableAdapterType;
    typedef RunnableAdapterType::Callback Callback;

    // construction

    Activity(
        C* pOwner,
        Callback method
        );

    // methods

    void
    start();

    void
    start(ThreadPool& pool);

    void
    stop();

    void
    wait();

    void
    wait(long milliseconds);

    bool
    isStopped() const;

    bool
    isRunning() const;

protected:
    // methods

    virtual
    void
    run();
};

Inherited Members

public:
    // methods

    virtual
    void
    run() = 0;

Detailed Documentation

This template class helps to implement active objects.

An active object uses threads to decouple method execution from method invocation, or to perform tasks autonomously, without intervention of a caller.

An activity is a (typically longer running) method that executes within its own task. Activities can be started automatically (upon object construction) or manually at a later time. Activities can also be stopped at any time. However, to make stopping an activity work, the method implementing the activity has to check periodically whether it has been requested to stop, and if so, return. Activities are stopped before the object they belong to is destroyed. Methods implementing activities cannot have arguments or return values.

Activity objects are used as follows:

class ActiveObject
{
public:
    ActiveObject():
        _activity(this, &ActiveObject::runActivity)
    {
        ...
    }

    ...

protected:
    void runActivity()
    {
        while (!_activity.isStopped())
        {
            ...
        }
    }

private:
    Activity<ActiveObject> _activity;
};

Construction

Activity(
    C* pOwner,
    Callback method
    )

Creates the activity.

Call start() to start it.

Methods

void
start()

Starts the activity by acquiring a thread for it from the default thread pool.

void
stop()

Requests to stop the activity.

void
wait()

Waits for the activity to complete.

void
wait(long milliseconds)

Waits the given interval for the activity to complete.

An TimeoutException is thrown if the activity does not complete within the given interval.

bool
isStopped() const

Returns true if the activity has been requested to stop.

bool
isRunning() const

Returns true if the activity is running.

virtual
void
run()

Do whatever the thread needs to do.

Must be overridden by subclasses.