Do I need to protect read access to an STL contain

2019-01-07 22:40发布

I have one std::list<> container and these threads:

  • One writer thread which adds elements indefinitely.

  • One reader/writer thread which reads and removes elements while available.

  • Several reader threads which access the SIZE of the container (by using the size() method)

There is a normal mutex which protects the access to the list from the first two threads. My question is, do the size reader threads need to acquire this mutex too? should I use a read/write mutex?

I'm in a windows environment using Visual C++ 6.

Update: It looks like the answer is not clear yet. To sum up the main doubt: Do I still need to protect the SIZE reader threads even if they only call size() (which returns a simple variable) taking into account that I don't need the exact value (i.e. I can assume a +/- 1 variation)? How a race condition could make my size() call return an invalid value (i.e. one totally unrelated to the good one)?

Answer: In general, the reader threads must be protected to avoid race conditions. Nevertheless, in my opinion, some of the questions stated above in the update haven't been answered yet.

Thanks in advance!

Thank you all for your answers!

8条回答
放我归山
2楼-- · 2019-01-07 23:16

You should consider some SLT implementation might calculate the size when called.
To overcome this, you could define a new variable

volatile unsigned int ContainerSize = 0;

Update the variable only inside already protected update calls, but you can read / test the variable without protection (taking into account you don't need the exact value).

查看更多
疯言疯语
3楼-- · 2019-01-07 23:17

I don't believe the STL containers are thread-safe, as there isn't a good way to handle threads cross-platform. The call to size() is simple, but it still needs to be protected.

This sounds like a great place to use read-write locks.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-07 23:25

I'm going to say no. In this case.

Without a mutex, what you might find is that the size() returns the wrong value occasionally, as items are added or removed. If this is acceptable to you, go for it.

however, if you need the accurate size of the list when the reader needs to know it, you'll have to put a critical section around every size call in addition to the one you have around the add and erase calls.

PS. VC6 size() simply returns the _Size internal member, so not having a mutex won't be a problem with your particular implementation, except that it might return 1 when a second element is being added, or vice-versa.

PPS. someone mentioned a RW lock, this is a good thing, especially if you're tempted to access the list objects later. Change your mutex to a Boost::shared_mutex would be beneficial then. However no mutex whatsoever is needed if all you're calling is size().

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-07 23:32

Yes, the read threads will need some sort of mutex control, otherwise the write will change things from under it.

A reader/writer mutex should be enough. But strictly speaking this is an implmentation-specific issue. It's possible that an implementation may have mutable members even in const objects that are read-only in your code.

查看更多
倾城 Initia
6楼-- · 2019-01-07 23:33

Whether or not size() is safe (for the definition of "safe" that you provide) is implementation-dependent. Even if you are covered on your platform, for your version of your compiler at your optimization level for your version of thread library and C runtime, please do not code this way. It will come back to byte you, and it will be hell to debug. You're setting yourself up for failure.

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-07 23:35

Checkout the concurrent containers provided by Intel's Open Source Threading Building Blocks library. Look under "Container Snippets" on the Code Samples page for some examples. They have concurrent / thread-safe containers for vectors, hash maps and queues.

查看更多
登录 后发表回答