Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-14 17:48:52


Michael Kenniston wrote:
>
> John Max Skaller and Bill Kempf wrote:
> > > I believe every single use of static variables
> > > and singletons is BAD DESIGN.
> > Well, I don't agree. <snip> there are several
> > classes of designs that simply can't be coded with out the use of
> > global data.
> > > Show me one. I've never seen one. I've never used
> > > global data (not since the '70's anyhow).
> > > [lexical scanner example snipped]
>
> While I don't have enough experience with singletons to
> convincingly argue either side, I do have enough experience
> to find this discussion very interesting, especially when it
> includes specific examples like the lexical scanner.
>
> Personally I'd like to see the debate continue, with additional
> cases proposed as requiring singletons or static variables, and
> rebuttals showing how/why they can/should be avoided (although
> perhaps we should let Mr. Kempf finish the threads library
> first :-).

        Just to be clear: Bill is including TLS in the library
for a very good reason with which I agree. I wish it were
not necessary to support 'bad' designs, but compatbility
is a very important issue, and being able to support
such designs is a sensible requirement: people will want to
take existing non-portable designs and make localized changes
to retrofit boost threads to make the design portable,
and if the design is already using OS TLS resources,
then the boost library must provide that too.

> We might not "resolve" the issue to everyone's
> satisfaction, but it could still be very enlightening and help
> many of us raise the level of our design skills. Even if nobody
> is brave enough to attempt an implementation of a boost singleton
> library right now (and I know I'm not), this discussion could lay
> the foundation for some documentation for it that supplies examples
> of when singletons should and should not be used and why.

        TLS is necessary: but of course, each thread has a stack
which is precisely that, so there is no intrinsic reason to
have any other kind. Shared data objects should be passed to threads
by the administration routine that knows which threads should
share what data with each other. Using static globals for this
is unnecessarily restrictive, and worse, it hides the fact
that threads are coupled: if I see

        create_thread(f);

where

        void f();

then there ought to be no way for 'f' to communicate data with anyone.
If that isn't the case, the design is flawed: you should have
PASSED the shared storage area, a channel, or something else
so f knows how to communicate. Otherwise, I can't trace
data flow by examining the interfaces: I have to read every
line of the implementation, to see if you've accessed a global variable
to get some data. That's bad design: the semantics should specify
how the interface works in terms of the arguments passed,
and no other (nonconstant) data.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden] 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net

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