help create events w/ boost

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe, G.I.

    help create events w/ boost

    I'm new to boost and what I'm doing seems very simple, but I can't get
    it. I want to store 3 events onto a priority_queue and have them execute
    n seconds from the time they were created, n is a randomly generated
    amount of time between 0 and 10 seconds.


    // create container that holds function objects
    priority_queue< boost::function <void (Event) pq;

    // create 3 events and place in priority_queue
    boost::bind(des troy_target, placeholders::_ 1, 'pq');
    boost::bind(des troy_target, placeholders::_ 1, 'pq');
    boost::bind(des troy_target, placeholders::_ 1, 'pq');


    I think I now have 3 function objects on the queue, but I don't know how
    to pull the Event object off the queue and check f it's timestamp is
    expired, and if so execute the function object that was stored on the queue.


    // get the first event off the priority_queue, and if expired, execute
    destroy_target( ).
    Event e = pq.top()(Event( ));

    // this is the function I eventually want my event to execute
    void destroy_target( ) { ... }



    Any help is greatly appreciated.
  • Jeff Schwab

    #2
    Re: help create events w/ boost

    Joe, G.I. wrote:
    I want to store 3 events onto a priority_queue and have them execute
    n seconds from the time they were created
    See below.
    priority_queue< boost::function <void (Event) pq;
    >
    // create 3 events and place in priority_queue
    boost::bind(des troy_target, placeholders::_ 1, 'pq');
    Each of those calls binds the first argument of destroy_target to the
    multi-character character constant 'pq'. That is probably not what you
    want. Use the priority_queue' s "push" method to insert the functions.

    Note that the new C++ draft standard includes std::function and
    std::bind (already available in recent compilers), so you probably don't
    need boost for this at all.
    I don't know how
    to pull the Event object off the queue and check f it's timestamp is
    expired
    You can use the std::priority_q ueue's top() method to access the object
    (and pop() to remove it from the queue), but where did you store the
    timestamp?
    // this is the function I eventually want my event to execute
    void destroy_target( ) { ... }
    That function has no arguments; to what were you binding that placeholder?

    Assuming the callback function takes no parameters, here is some naive,
    POSIX-specific code that shows how you could implement such a timed
    callback mechanism using boost::function :

    /* POSIX */
    #include <unistd.h>

    /* Boost */
    #include <boost/function.hpp>

    /* C++ Standard */
    #include <ctime>
    #include <functional>
    #include <iostream>
    #include <queue>

    namespace {

    typedef std::time_t time_type;

    void sleep(unsigned seconds =1) {

    /* POSIX ::sleep(unsigne d int) is declared in unistd.h. */

    ::sleep(seconds );
    }

    time_type current_time() {

    using std::time;

    return time(0);
    }

    struct scheduler {

    typedef boost::function <void ()function;

    private:

    struct scheduled_funct ion {

    time_type when;
    function what;

    scheduled_funct ion(
    time_type time,
    function const& func ):
    when( time ),
    what( func ) { }
    };

    friend bool operator<(
    scheduled_funct ion const& a,
    scheduled_funct ion const& b) {
    return a.when b.when;
    }

    std::priority_q ueue<scheduled_ functionqueue_;

    public:

    void at(time_type when, function const& what ) {
    queue_.push(sch eduled_function ( when, what ));
    }

    void run() {

    for (; !queue_.empty() ; sleep()) {

    time_type const now = current_time();

    while (!queue_.empty( ) &&
    queue_.top().wh en <= now) {
    queue_.top().wh at();
    queue_.pop();
    }
    }
    }
    };
    }

    #include <iostream>

    template<int I>
    void print() {
    std::cout << I << '\n';
    }

    int main(int argc, char const** argv) {

    scheduler sched;
    time_type const now = current_time();

    sched.at(now + 1, print<1>);
    sched.at(now + 2, print<2>);
    sched.at(now + 3, print<3>);
    sched.run();

    return 0;
    }

    Comment

    Working...