Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-06-08 10:38:40


From: Michael Glassford <glassfordm_at_[hidden]>
> Rob Stewart wrote:
>
> > Do you really want to account for the creation and joining of
> > threads outside of main()? I'm no MT expert, but I recall
> > learning that one should avoid creating threads prior to main.
> > It makes statics and even main() difficult to write. Isn't
> > that correct? If so, should Boost.Thread really account
> > for bad designs?
>
> The biggest problem with this is that global static objects are
> initialized before main(). Is it necessarily a bad thing for the
> constructor of such an object to create a thread?

My understanding is that it is a problem, but I may be wrong.
The problems you run into with permitting threads before main()
is that you have to make access to shared resources thread safe
when accessed by static objects that can be created in any order
and main() can't do any required initialization without worrying
about thread safety. The better approach is for statics to be
initialized in a single thread and for main() to do its
initialization and, only then, does main() cause the various
threads to come into existence. That is, main() doesn't need to
create the threads, but it does need to make the (member)
function calls necessary to do so.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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