Boost logo

Boost :

From: Arash Partow (arash_at_[hidden])
Date: 2006-07-14 10:29:46


Hi Peter,

thanks for your help, I'm one step closer to what I've been trying
to accomplish and that is to be able use bind's within threads,
the problem it seems is that bind doesn't like classes that implement
the noncopyable interface.

Would you have any idea on how to resolve such an issue?
My code is as follows:

template <typename T,int i> class select
{
public:
   typedef typename boost::tuples::element<i,T>::type result_type;

   template <typename U>
   result_type operator()(const U& u) const
   {
     return u.get<i>();
   }

};

inline void do_something(const int i, const int j)
{
    for(unsigned int x = 0; x < 1000; x++)
    {
       std::cout << i << "\t" << j << std::endl;
    }
}

int main(int argc, char* argv[])
{
    typedef boost::tuples::tuple<int,int> param_set;

    std::vector<param_set> list;
    list.push_back(param_set(1,2));
    list.push_back(param_set(2,3));
    list.push_back(param_set(3,4));

    boost::thread_group thrd_grp;

     std::for_each(list.begin(),
                   list.end(),
                   boost::bind(&boost::thread_group::create_thread,thrd_grp,
                   boost::bind(&do_something,
                                boost::bind(select<param_set,0>(),_1),
                                boost::bind(select<param_set,1>(),_1))));

    thrd_grp.join_all();
    return true;
}

Arash Partow
________________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net

Peter Dimov wrote:
Arash Partow wrote:
> > Hi all,
> >
> > I'm having some difficulties getting the following piece of code to
> > compile:
> >
> >
> > #include <iostream>
> > #include <vector>
> >
> > #include <boost/utility.hpp>
> > #include <boost/bind.hpp>
> > #include <boost/tuple/tuple.hpp>
> >
> >
> > class AClass
> > {
> > public:
> > AClass(){}
> > void b(int i, int j, int k)
> > {
> > std:: cout << "a::b() - " << i << "\t" << j << "\t" << k <<
> > std::endl; }
> > };
> >
> > template <typename T,int i> class select
> > {
> > public:
> > typedef typename boost::tuples::element<i,T>::type result_type;
> >
> > template <typename U>
> > result_type operator()(const U& u) const

The simpler

    result_type operator()(const T& u) const

ought to work, too.

> > {
> > return u.get<i>();

You might need u.template get<i>(); here depending on the compiler.

> > }
> >
> > };
> >
> > int main(int argc, char* argv[])
> > {
> > typedef boost::tuples::tuple<AClass,int,int,int> param_set;
> >
> > std::vector<param_set> list;
> > list.push_back(param_set(AClass(),1,2,3));
> > list.push_back(param_set(AClass(),2,3,4));
> > list.push_back(param_set(AClass(),3,4,5));
> > list.push_back(param_set(AClass(),4,5,6));
> > list.push_back(param_set(AClass(),5,6,7));
> >
> > std::for_each(
> > list.begin(),
> > list.end(),
> > boost::bind(&AClass::b,
> > boost::bind(select<param_set,0>(),_0),

There is no placeholder named _0, they start at _1.

> > boost::bind(select<param_set,0>(),_1),
> > boost::bind(select<param_set,0>(),_2),
> > boost::bind(select<param_set,0>(),_3)));

You don't have enough arguments to use _2 or _3 as for_each only passes one.
Instead of trying to get<0> the first element of the four tuples _0.._3, you
need to get<0..3> the four elements of the one tuple _1.

                                boost::bind(select<param_set,0>(),_1),
                                boost::bind(select<param_set,1>(),_1),
                                boost::bind(select<param_set,2>(),_1),
                                boost::bind(select<param_set,3>(),_1)));


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk