Re: Request for comments about synchronized queue using boost
On Oct 22, 7:04 pm, gpderetta <gpdere...@gmail.com> wrote:
On Oct 22, 6:41 pm, James Kanze <james.ka...@gmail.com> wrote:
On Oct 17, 10:24 am, Szabolcs Ferenczi <szabolcs.feren...@gmail.com>
wrote:
You should not stop by the first fragment which is just a
starting point illustrating the problem.
When I see threaded code returning a reference into a
protected structure, after having freed the lock, I stop.
No one who understands threading would ever write something
like that.
Well, the queue is a single consumer object (the author
explicitly says that), so as long as the consumer doesn't pop,
the reference is never invalidated (it uses a deque
internally), at least with all sane implementations of deque
(and I'm fairly sure that c++0x will require it to work).
In other words, if it works, it works. The author does vaguely
mention something about "single consumer" after presenting the
code, but there's certainly nothing fundamental in his code
which prevents the client from reading from several different
threads. *IF* you're claiming any sort of thread safety
contract which doesn't require external synchronization, you
don't allow pointers and references to external data to escape.
I agree in that he should not have suggest such a bad
habit of handling a shared resource in the front part of
his article or, at least, he should have warned the
smattering reader that it is not the correct way.
The rest just goes on to present what everyone knows anyway.
'Everyone' is a fairly strong word. In my experience, many
programmers have no idea of what a condition variable is or
how to use it.
Yes. I should have said: everyone who knows anything about
programming with threads. From what I've seen, that's really a
minority.
My problem with the article is twofold: first, he presents a
simplistic implementation which is simply too dangerous to
consider. He does mention a vague constraint concerning its
use, but without explaining why, or covering any of the basic
principles. And he then goes on to explain how to use one
particular construct, a condition, again without explaining any
of the basic details. If someone reads just that article,
they're going to mess things up seriously, because they don't
know the basic principles. And if they've learned the basic
principles, say by reading Butenhof, then they already know
everything the article presents.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34