Topic: stl thread safety
Author: "Ask" <ask.q@indiatimes.com>
Date: Fri, 2 Dec 2005 00:11:47 CST Raw View
Do the standards say that STL implementation be thread safe ? or, are
there options available (in general) to choose thread safe/unsafe
implementation of STL for a particular platform ?
Thanks
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: no.spam@no.spam.com (Maciej Sobczak)
Date: Sat, 3 Dec 2005 01:32:45 GMT Raw View
Ask wrote:
> Do the standards say that STL implementation be thread safe ?
No. In general, standard does not say anything about threads at all.
But it does not matter, because you did not explained what do you mean
by "thread safe" STL implementation.
From the quality implementation you can expect the following:
1. STL algorithms can be called from many threads (but see 1. and 2. below).
2. STL containers can be read (directly or with algorithms, see 1.) by
many threads, if you can guarantee that no thread modifies the same
container at the same time.
3. STL containers *cannot* be modified by many threads at the same time.
You can do some tricks to relax these constraints (for example, two
threads modifying distinct elements of the container might be OK, two
threads inserting or removing elements from std::list might be OK (if
those elements are "far enough" from each other), etc.), but no matter
what you do, it's your responsibility to *define and provide*
appropriate granularity of synchronization, where the meaning of
"appropriate" depends on what you actually do.
STL itself will not do anything on your behalf here, and that's good.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ben-public-nospam@decadentplace.org.uk (Ben Hutchings)
Date: Sat, 3 Dec 2005 01:34:06 GMT Raw View
Ask <ask.q@indiatimes.com> wrote:
> Do the standards say that STL implementation be thread safe ? or, are
> there options available (in general) to choose thread safe/unsafe
> implementation of STL for a particular platform ?
The current C++ standard specifies a single thread of execution. Any
support for multithreading is, at present, a non-standard extension.
You must refer to implementation-specific documentation to find
thread-safety guarantees (and even there you may often find they are
regrettably under-specified).
--
Ben Hutchings
Man invented language to satisfy his deep need to complain. - Lily Tomlin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Sat, 3 Dec 2005 01:34:21 GMT Raw View
In article <1133502095.188482.137910@z14g2000cwz.googlegroups.com>, Ask
<ask.q@indiatimes.com> writes
>Do the standards say that STL implementation be thread safe ? or, are
>there options available (in general) to choose thread safe/unsafe
>implementation of STL for a particular platform ?
As the current standard is completely agnostic on the subject of threads
no such guarantee is made.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kuyper@wizard.net
Date: Fri, 2 Dec 2005 19:34:08 CST Raw View
Ask wrote:
> Do the standards say that STL implementation be thread safe ? or, are
> there options available (in general) to choose thread safe/unsafe
> implementation of STL for a particular platform ?
The current standard doesn't address threads at all. However, as a
simple matter of practical reality, some implementors have been
attempting to provide thread-safety in their libraries, though I can't
say anything about how well they've handled the task. I believe that
people are working on proposals for addressing threads in the next
release of the standard.
Minor issue: STL existed before the standard, and while much of STL was
incorporated into the C++ standard library, the standard never refers
to it as STL, and I've been told that it's wrong to use that term to
describe those templates. In principle, the phrase "C++ standard
template library", without uppercase abbreviation, would correctly
describe everything that was brought into C++ from STL, but it would
also cover a few other things as well.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Michiel.Salters@tomtom.com
Date: Fri, 2 Dec 2005 19:34:04 CST Raw View
Ask wrote:
> Do the standards say that STL implementation be thread safe ?
The standard doesn't say anything about threads.
> or, are there options available (in general) to choose thread safe/unsafe
> implementation of STL for a particular platform ?
Varies. E.g. VC6 had the choice, but I think VC8 no longer has
thread-unsafe
versions. (The overhead simply became too small to warrant the extra
compiler complexity, IIRC)
Regards,
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Tom Widmer" <tom_usenet@hotmail.com>
Date: Fri, 2 Dec 2005 19:34:12 CST Raw View
Ask wrote:
> Do the standards say that STL implementation be thread safe ? or, are
> there options available (in general) to choose thread safe/unsafe
> implementation of STL for a particular platform ?
The standard has no concept of threads, so it doesn't mention thread
safety. This may be addressed in a future standard (C++ 0x).
As for the practical reality, read these pages:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfThreadSafetyInStandardCLibrary.asp
http://www.sgi.com/tech/stl/thread_safety.html
http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#3
Tom
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: pjp@dinkumware.com ("P.J. Plauger")
Date: Sat, 3 Dec 2005 01:50:34 GMT Raw View
"Ask" <ask.q@indiatimes.com> wrote in message
news:1133502095.188482.137910@z14g2000cwz.googlegroups.com...
> Do the standards say that STL implementation be thread safe ? or, are
> there options available (in general) to choose thread safe/unsafe
> implementation of STL for a particular platform ?
The C++ Standard is silent on the subject of multithreading.
Most libraries these days are thread safe in the sense
promulgated by SGI:
-- Multiple threads can safely read the same object
(no unprotected mutables).
-- Writing an object in one thread has no surprising
effect on a different object of the same type in
another thread (no unprotected shared writable statics).
-- The obvious thread synchronization suffices to
protect multiple threads reading and writing the
same object.
HTH,
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: jgottman@carolina.rr.com ("Joe Gottman")
Date: Sat, 3 Dec 2005 07:46:09 GMT Raw View
"Maciej Sobczak" <no.spam@no.spam.com> wrote in message
news:dmp1p7$3g5$1@sunnews.cern.ch...
> Ask wrote:
>
>> Do the standards say that STL implementation be thread safe ?
>
> No. In general, standard does not say anything about threads at all.
>
> But it does not matter, because you did not explained what do you mean by
> "thread safe" STL implementation.
>
> From the quality implementation you can expect the following:
>
> 1. STL algorithms can be called from many threads (but see 1. and 2.
> below).
>
> 2. STL containers can be read (directly or with algorithms, see 1.) by
> many threads, if you can guarantee that no thread modifies the same
> container at the same time.
>
> 3. STL containers *cannot* be modified by many threads at the same time.
>
You forgot one:
4. Different STL containers can be safely modified by different threads at
the same time.
Joe Gottman
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "kanze" <kanze@gabi-soft.fr>
Date: Tue, 6 Dec 2005 21:35:12 CST Raw View
Maciej Sobczak wrote:
> Ask wrote:
> > Do the standards say that STL implementation be thread safe ?
[...]
> From the quality implementation you can expect the following:
> 1. STL algorithms can be called from many threads (but see 1.
> and 2. below).
> 2. STL containers can be read (directly or with algorithms,
> see 1.) by many threads, if you can guarantee that no thread
> modifies the same container at the same time.
> 3. STL containers *cannot* be modified by many threads at the
> same time.
All of this depends on the implementation; not all
implementations give the same guarantee. G++, for example, only
gives 2 for the parts of the library which are lifted directly
from the SGI implementation -- the explicitly state that it
doesn't hold for other objects.
The guarantees which you state are basically the SGI guarantees.
They hold for the SGI implementation. I imagine that other
implementations have also adopted them, but I wouldn't count on
it without explicit documentation from the implemenation.
--
James Kanze GABI Software
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]