|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-07-02 13:28:32
From: <williamkempf_at_[hidden]>
> > 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();
>
> Ooops... that's what I get for doing this on the fly. You need the
> following line as well:
>
> delete *it;
> > it = v_.erase(it);
> > }
> > }
> >
> > private:
> > std::vector<thread*> v_;
> > };
>
> I should also note that this is a poor implementation of a
> thread_group, the destruction and removal from the group should
> probably only be done in the object's destructor and no during the
> call to join_all().
Yep. And some of the implicit members are neither fine nor useful. :-)
This is not meant to pick on your ability to write bug-free code on the fly
in an e-mail. Nobody can do that.
But if you consider "Bill Kempf/Peter Dimov writing code on the fly in an
e-mail" as a rough measure of what the "average" C++ programmer is likely to
come up with, you'll see that this "unofficial thread_group test" does
provide some indications as to which design is less error prone and easier
to use.
-- 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