Boost logo

Boost Users :

From: Edd Dawson (lists_at_[hidden])
Date: 2007-07-11 15:29:19


Hi folks,

I'm currently attempting to create a little facility that will allow me
to eaily kick off a function call in another thread and get the result
through a "future". Something like:

unsigned count_primes_upto(std::size_t max);

async::layered_cage
<
    std::exception,
    std::domain_error,
    async::catch_all
>
cage; // to catch exceptions

// call count_primes_upto(9876543210) in another thread
async::future<unsigned> n =
    async::call(cage, &count_primes_upto, 9876543210);

// do other stuff in the mean time
// ...

// Block until done. Get result or propagate exception
std::cout << "there are " << n.value() <<
             " primes less than " << 9876543210 <<'\n';

To achieve this I'm using boost::bind and boost::function quite heavily.
I've discovered that by the time the final boost::function<void ()> is
composed for the boost::thread constructor, I've copied each of the
arguments (such as 9876543210 in the above) about 30 times!.

Now, I can use boost::ref() internally in such a way that no copying of
arguments happens at all. However, this is also undesirable; I would
like the thread that is created to have its own copy of each argument
unless the client explicitly wraps an argument in boost::ref/cref in
order to avoid dangling cross-thread references.

So what I'd like is a way to copy the result of my final boost::bind()
call in to a new functor where all the ref() arguments are deep-copied
once and only once at the end.

Is there some way of doing this? Perhaps the "as yet undocumented"
visit_each is relevant, here?

Kind regards,

Edd


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net