Does deleting and pasting to a stream with a single linked list remove?

Using std :: forward_list, are there any data races when erasing and pasting? For example, I have one thread that does nothing but add new items at the end of the list, and I have another thread that goes on the (same) list and can erase items from it.

From what I know about linked lists, each element contains a pointer to the next element, so if I delete the last element at the same time as I insert a new element, this will cause data to race or make this containers work differently (or are they coping with this opportunity)?

If it's a data race, is there a (simple and quick) way to avoid this? (Note: the thread that inserts is most critical for speed.)

+4
source share
4 answers

There are thread safety guarantees for standard C ++ library containers, but they usually do not relate to how people will view thread safety guarantees (that is, however, a mistake made by people expecting the wrong thing). Potential security guarantees for standard library containers are approximately (corresponding section 17.6.5.9 [res.on.data.races]):

  • , . , , , const member, const ( , .. 23.2.2 [container.requirements.dataraces] , , ).
  • , .

, ! , , . , , erase() node insert() s, .

+6

, forward_list, STL- . , . .

- . ++ 11 (std:: mutex) ( Windows, pthreads Linux/Unix).

+3

STL, , -, , .

, , , , , .

, , .

+2
source

The std containers should not be thread safe.

You must carefully protect them for modification operations.

+1
source

All Articles