Boost logo

Boost :

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


From: <williamkempf_at_[hidden]>
> --- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:

> > 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.
>
> There are two problems here. First, as I pointed out the details are
> hidden in the other concept, so the ease of use is not impacted.

Ease of use - provided you have a working thread_group - if of course not
impacted. Implementing a thread_group does, however, demonstrate the ease of
use of the base design.

> Second, you chose to use a ref-count design internally, when such a
> design is not needed.
>
> class thread_group
> {
> public:
> // all implicit members are fine and useful
>
> template<class F> void create(F f)
> {
> v_.push_back(new thread(f));
> }
>
> void join_all()
> {
> std::vector<thread*>::iterator it = v_.begin();
> while (it != v_.end())
> {
> (*it)->join();
> it = v_.erase(it);
> }
> }
>
> private:
> std::vector<thread*> v_;
> };
>
> You should note that like you I didn't include proper
> synchronization, so this implementation isn't valid, but it
> illustrates the point.

You didn't say anything about sharing a thread_group between threads, so a
non-synchronized thread_group still matches the ad-hoc specification given
in your 'void foo()' example.

> My implementation required no reference
> management, only explicit lifetime management.

And explicit management is error prone, as you have demonstrated.

--
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