[ create a new paste ] login | about

Link: http://codepad.org/j4mibHWS    [ raw code | fork ]

C++, pasted on Feb 26:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/thread.hpp>
#include <vector>

namespace bl = boost::lambda;

template<typename ftor, typename data>
class Foo
{
public:
    explicit Foo()
    {
    }
    void doFtor ()
    {
        // The boost::ref here is necessary only if Ftor::operator() is not const
        _threads.create_thread(bl::bind(boost::ref(_ftor), _list.begin(), _list.end()));
        _threads.join_all();
    }

private:
    boost::thread_group _threads;
    ftor _ftor;
    std::vector<data> _list;
};

template<typename data>
class Ftor
{
public:
    typedef void result_type;

    explicit Ftor () {}

    // Note that the iterators are passed by copy
    // As written, this operator could very well be const, as it does not modify the state
    // of the Ftor object. In that case, it may be simpler to add "const" here------------------------+
    // and to remove the boost::ref in Foo::doFtor                                                    |
    void operator() (typename std::vector<data>::iterator startItr, typename std::vector<data>::iterator endItr)
    {
        std::for_each(startItr, endItr, std::cout << bl::_1 << bl::constant("."));
    }
};

int main(void)
{
    Foo<Ftor<int>, int> f;
    f.doFtor();

    return 0;
}


Create a new paste based on this one


Comments: