Afficher un message
Vieux 04/04/2008, 20h27   #7
Chris Thomasson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL container read thread-safety.

"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:kguJj.20$_71.13@newsfe02.lga...
> jason.cipriani@gmail.com wrote:
>> I know that the overall thread safety of STL containers is not
>> guaranteed. However, if I have an std::vector<int> or an
>> std::list<int>, and multiple threads are reading them but nothing is
>> writing them, do I still need to synchronize access to them? By
>> "reading" I mean iterating through with const_iterators, and also
>> copying them to other containers with the assignment operator.
>>
>> What I mean is, am I guaranteed that, say, I don't know, obtaining a
>> const_iterator via std::list<int>::begin() isn't doing any internal
>> housekeeping or anything that would potentially make it not thread-
>> safe?
>>
>> What about set and map?

>
> If "nothing is writing them" is indeeed true, you *should* be okay most of
> the time. However, how can you be 100% sure that something won't write to
> it somewhere? Somehow you need to get the entries into the container for
> your threads to read. If something is writing to a container while
> something else is reading it, bad things happen.
>
> It is much eaiser to wrap the container in a thread safe class then to
> make sure your code doesn't write to a container when something is reading
> it.


Right. I would advise to read the documentation of the STL and find out if
concurrent reads are okay. If they are, then use an external rwmutex, wrap
the container with one, for synchronization. All mutators get write access,
everything else gets read access.

  Réponse avec citation
 
Page generated in 0,06402 seconds with 9 queries