|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-12-16 09:26:31
mark_2811_at_[hidden] wrote:
> Hi, I'm new to Boost and am interested in what some of the libraries
> can do to improve my programs. Part of my program involves an Active
> Object that takes calls to methods made in one thread and executes
> them in another. The typical way I've done this in the past is to
> use the Command pattern to write commands onto a message queue that
> the other thread remove from the queue and execute. I'm wondering if
> boost::function can be used to accomplish the same thing with less
> code to write since the Command pattern requires one class for each
> method that is called. Is this a common use for boost::function?
> I'd assume that arguments must be bound using boost::bind? Pretty
> much all of my methods will have arguments that will need to be bound
> when the method is initially called. From my initial read of the
> documentation it seems that the bind library has some limitations
> (call by reference only) so I'm not sure of it's usefulness in this
> scenario?
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <list>
#include <iostream>
void f(int x)
{
std::cout << "f(" << x << ")\n";
}
void g(double x)
{
std::cout << "g(" << x << ")\n";
}
typedef boost::function< void() > command;
std::list<command> commands;
void producer()
{
commands.push_back( boost::bind(f, 1) );
commands.push_back( boost::bind(g, 2.4) );
commands.push_back( boost::bind(f, 3) );
}
void consumer()
{
while( !commands.empty() )
{
commands.front()();
commands.pop_front();
}
}
int main()
{
producer();
consumer();
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk