Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-07-02 12:52:13


From: <williamkempf_at_[hidden]>

> --- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:

> > If you expand the examples that had a thread_manager or a
> thread_group in
> > them, giving the full implementation, you'll see that they, too,
> demonstrate
> > the same point.
>
> No, they don't, actually. Any difficulties in use (minimal) are
> eliminated by the other concepts.

Let's get back to your original example:

void foo()
{
   thread_group threads;
   for (int i=0; i<10; ++i)
      threads.create(&bar);
   threads.join_all();
}

Here's an implementation of thread_group:

class thread_group
{
public:

    // all implicit members are fine and useful

    template<class F> void create(F f)
    {
        v_.push_back(thread::create(f));
    }

    void join_all()
    {
        thread::join(v_.begin(), v_.end());
    }

private:

    std::vector<thread::ref> v_;
};

As you can see, it's very similar to my vector example.

> > So, your unspoken question is probably "why provide a thread::ref
> when we
> > already have shared_ptr?"
>
> No, that's not really the issue. Ref-counting is the exception, not
> the rule, to the need for lifetime management. Explicit ownership is
> the norm, which is also much more efficient.

Is it really your opinion that explicit management is the norm in modern
C++?

--
Peter Dimov
Multi Media Ltd.

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