INTRODUCTION
Overview
Download and Install
Quick Start
Documentation
Publications

NONFRAMEWORK CODE
Driver Interfaces
Drivers
Libraries
Utilities

FRAMEWORK CODE
Interfaces
Components
Libraries
Utilities

Full Software Listings

DEVELOPER
Tutorials
Examples
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         

hydroiceutil::EventQueue Class Reference

Thread-safe event queue. More...

#include <eventqueue.h>

List of all members.

Public Member Functions

 EventQueue (bool traceAddEvents=false, bool traceGetEvents=false)
 Set tracing flags to TRUE to print messages to standard output.
void setOptimizer (EventQueueOptimizerPtr optimizer)
 Set the optimizer.
void add (const EventPtr &e)
 Add event to the queue.
void add (int type)
 Add event of type type to the queue.
void optimizedAdd (const EventPtr &e)
void get (EventPtr &e)
bool timedGet (EventPtr &e, int timeoutMs)
void clear ()
 Discards all events.
int size () const
 Number of event in the queue.

Detailed Description

Thread-safe event queue.

This class does not execute the events. For an event execution loop see EventLoop.

First define some custom events.

#include <hydroiceutil/eventqueue.h>

enum EventType { Activate };

class ActivateEvent : public hydroiceutil::Event
{
public:
    ActivateEvent( bool urgent=false ) : hydroiceutil::Event( Activate ), isUrgent(urgent) {};
    bool isUrgent;
};
typedef IceUtil::Handle<ActivateEvent> ActivateEventPtr;

Your threaded class will have an event queue, probably as a member variable.

hydroiceutil::EventQueuePtr events_;

Don't forget to initialize it in the constructor

events_(new hydroiceutil::EventQueue)

When running inside your thread, read from the queue, with or without timeout. Here's an example of doing it inside the run function of gbxiceutilacfr::Thread.

void run()
{
    hydroiceutil::EventPtr event;
    int timeoutMs = 500;
    
    while ( !isStopping() )
    {
        if ( !events_->timedGet( event, timeoutMs ) ) {
            // timed out
            continue;
        }
    
        switch ( event->type() )
        {
            // approx in order of call frequency
            case Activate : {
                ActivateEventPtr e = ActivateEventPtr::dynamicCast( event );
                if ( e->isUrgent )
                    // do something
                else
                    // don't do anything
                break;
            }
            default : {
                cout<<"Unknown event "<<event->type()<<". Ignoring..."<<endl;
            }
        } // switch
    } // while
}

Asynchronously from your main thread loop, you can add events to your queue.

void outsideFunctionCall()
{
    events_->add( new ActivateEvent() );
}
See also:
EventQueueHolder

Member Function Documentation

void EventQueue::get ( EventPtr e  ) 

Get event from the queue. If the queue is empty this call blocks indefinitely until an event is added.

void EventQueue::optimizedAdd ( const EventPtr e  ) 

Add event to the queue but, before the event is added, the queue tries to combine it with the last event already in the queue. This is done by calling combine() function of the queue's optimizer. The default optimizer does not support any combinations. To implement combination(s) for your events, write your own optimizer by deriving from EventQueueOptimizer. Calling this function when the event is empty or when the queue's optimizer is not set or when the combine() operation fails for some reason is equivalent to calling add().

bool EventQueue::timedGet ( EventPtr e,
int  timeoutMs 
)

Get event from the queue with timeout. Returns TRUE if event was received, FALSE if timeout occured.

References hydroiceutil::now().


The documentation for this class was generated from the following files:
 

Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)


Generated for Orca Robotics by  doxygen 1.4.5