Re: Concurrent Containers
On 2010-09-01 15:38:00 -0400, Scott Meyers said:
On 9/1/2010 11:50 AM, Chris M. Thomasson wrote:
"Scott Meyers"<NeverRead@aristeia.com> wrote in message
If I'm a cross-platform C++ developer who wants to take advantage of the
work others have done to develop, debug, and tune a concurrent container
(i.e., I don't want to write and maintain my own), what are my choices?
I am VERY sorry that I simply have no time to explain the details right now,
but FWIW C++0x actually does provide a perfect medium for creating fairly
efficient garbage collected concurrent containers.
But that's not the question. C++ offers the capabilities to write
anything I want, but if I want to use a container that's already
written and is likely correct, I can use any of the containers in the
standard library, as long as I don't care about the ability to
concurrently modify the container. Most of the time, it's just not
worth my trouble to write a container from scratch.
Most of the time, being able to concurrently modify the contents of a
container isn't the right level of granularity. For a message queue,
it's what you need: inserting at one end and removing from the other
end has to work correctly. But for most problems, you need a
combination of operations to be atomic, so you need a higher level
lock. My favorite example (although not a container) is:
std::cout << "Hello, " << "world\n";
Doing this same write from multiple threads won't work right,
regardless of whether stream inserters are synchronized. The entire
statement has to be sychronized so that you don't get interleaved
output:
Hello, Hello, world
world
Similarly (or maybe dissimilarly), this code
if (iterator it = assoc_container.find(x))
assoc_container.erase(it);
won't work right if it isn't synchronized with code that can modify
assoc_container from another thread (since the other thread might erase
the found object between the call to find() and the call to erase()).
So, yes, it would probably be good for the standard library to provide
a synchronized queue; beyond that, synchronized containers would simply
be traps for the unwary. They would seem to promise something that they
can't deliver.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)