Boost logo

Boost :

Subject: Re: [boost] Boost.Future & review of C++0x accepted librariesimplementations
From: Vicente Botet (vicente.botet_at_[hidden])
Date: 2008-11-20 17:41:56


----- Original Message -----
From: "Anthony Williams" <anthony.ajw_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, November 20, 2008 10:28 PM
Subject: Re: [boost] Boost.Future & review of C++0x accepted librariesimplementations

>
> k-oli_at_[hidden] writes:
>
>> Am Donnerstag, 20. November 2008 18:19:12 schrieb Anthony Williams:
>>> k-oli_at_[hidden] writes:

>> X x;
>> jss::packaged_task< std::string > tsk( boost::bind( & X::execute, x) );
>> jss::shared_future< std::string > f( tsk.get_future() );
>> boost::thread t(
>> boost::bind(
>> & jss::packaged_task< std::string >::operator(),
>> tsk) );
>> t.join();
>> std::cout << f.get() << std::endl;
>>
>> Does not compile because packaged_task is noncopyable.
>> In the C++0x working draft std::move( tsk) was used - but how to do it with
>> the current boost release?
>
> boost::thread t(boost::move(tsk));

Hi,

Now that we have futures and packaged_task we are not too far from the Kevin proposal so the final use can write

    joiner<std::string> j = threader::launch_in_thread(bind( & X::execute, x));
    std::cout << j.get() << std::endl;

instead of

    packaged_task< std::string > tsk( bind( & X::execute, x) );
    shared_future< std::string > f( tsk.get_future() );
    thread t(move(tsk));
    t.join();
    std::cout << f.get() << std::endl;

Note. This code has of course not compiled.

class threader {
    public:
        template <typename Nullary>
        joiner<result_of<Nullary> > launch_in_thread(Nullary f) {
            return joiner<result_type>(f);
        }
};

template <typename ResultType>
class joiner {
    struct data {
        shared_future< ResultType > fut_;
        thread th_;

        template <typename Nullary>
        data()
            packaged_task<ResultType> tsk(f);
            fut_ = tsk.get_future();
            th_ = boost::move(tsk));
    }
    shared_ptr<data> data_;
public:
    template <typename Nullary>
    // requires result_of<Nullary>::type is equal to ResultType
    joiner(Nullary f) : data_(new data(f) {}
    // move semantics from a shared_ptr
    // ....
    void join() {th_.join();}
    ResultType get() {th_.join(); return fut_.get();}
};

Best,
Vicente


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