Access to std :: list from one or more threads

I need to access (read only) data std::listfrom one or more threads running in my application.

This list will be created only once upon loading the application, and after the entire list has been read

from multiple threads ... let's say I will do it

for (std::list <iterator>iii=list->begin();ii!=list->end();ii++)

which reads a list of multiple threads.

Now my question is, will it create a problem with violation of access rights if I did not synchronize access to the list?

I ran the application without any synchronization simulators. I did not encounter a problem. but since I doubt it, I would like to confirm it.

According to my understanding, even in a mutithreading application, only one thread is executed at runtime. because I do not update the list from muliple streams, and all streams read the list, which may not be required to provide exclusive access to the list .. is this correct?

please put me right if i'm wrong

+3
source share
5 answers

You are right if you only perform a read operation on a list object without any write operations that you cannot run into. However, note that in the case of processors with multiple cores, multiple threads can execute simultaneously.

+5
source

, , , . , , , , , , , .

+2

- -const. , .

+2

, init, std::vector, init.

0

... , , . , , ?

. , /.

mutithreading OS.

, /, . , - /.

/ , . , , , .

, , .. ?

- , .

Otherwise, if, for example, the list is populated in the main threads, before the worker threads are started, then it perfectly lowers the lock. And this is just a scenario where I would not use a lock. If the list is populated with another thread when many threads are already running, I would already add a lock: problems with multiple threads inside STL containers are hell for debugging, while a read / write lock has some minor overhead.

0
source

Source: https://habr.com/ru/post/1757296/


All Articles