Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-08-06 09:36:43


From: "Pete Becker" <petebecker_at_[hidden]>
> At 08:48 AM 8/6/2002 -0400, Moore, Dave wrote:
> >I think the idea of parameterizing the return value has merit on the
> >"syntactic sugar" side of things, as the user doesn't have to create
their
> >own boost::function just so they can extract a return value - they can
still
> >pass simple function pointers.
>
> Function pointers are a C solution, not a C++ solution.

Well, function objects are a C++ solution.

> Rather than beefing
> up the C solution, I suggest a more object-oriented solution: a class with
> a member function that runs in a separate thread. These have been around
> for years, dating from long before the days of Java:
>
> class thread
> {
> public:
> thread();
> ~thread();
> void start();
> private:
> virtual void run() = 0;
> };
>
> Derived classes can hold whatever data they need. When the thread
finishes,
> the object's data can be examined to get the result.

The important difference I see is that ~thread() needs to perform an
implicit join, since run() needs to access 'this'.

The current approach makes a copy of the function object, and ~thread
doesn't need to join().

Apart from that:

void join_and_delete(boost::thread * pt)
{
    pt->join();
    delete pt;
}

class oo_thread: noncopyable
{
private:

    shared_ptr<boost::thread> pt;

public:

    void start()
    {
        pt.reset(new boost::thread(bind(&oo_thread::run, this)),
join_and_delete);
    }

private:

    virtual void run() = 0;
};


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