[multi_index_container] Thread safety
Hi, As per the section "Complexity and exception safety": Iterator and reference validity is preserved in the face of insertions, even for replace and modify operations. Would it be safe to insert() from within two or more threads simultaneously ? Assumption being that the keys inserted will be always be different. -- regards, Prashant Thakre
Hello Prashant, Prashant Thakre ha escrito:
Hi, As per the section "Complexity and exception safety": Iterator and reference validity is preserved in the face of insertions, even for replace and modify operations.
Please note that exception safety is in no way related to thread safety.
Would it be safe to insert() from within two or more threads simultaneously ? Assumption being that the keys inserted will be always be different.
No, it is not safe to insert from two threads into the same container. The thread safety guarantees aplying to Boost.MultiIndex (and to most STL implementations as well) are: 1. Concurrent access to different containers is safe. 2. Concurrent read-only access to the same container is safe All other forms of concurrent access are *not* safe, for instance: * Concurrent write access to the same container (your scenario) is not safe. * One thread writes to a container and other(s) read(s) from the same container: not safe either. So you've got to synchronize access to the container as appropriate to your app semantics.
regards, Prashant Thakre
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
On 10/5/06, Joaquín Mª López Muñoz
No, it is not safe to insert from two threads into the same container. The thread safety guarantees aplying to Boost.MultiIndex (and to most STL implementations as well) are:
1. Concurrent access to different containers is safe. 2. Concurrent read-only access to the same container is safe
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I know the standard says nothing about thread-safety (and I also know they are thinking about saying a few things next time), but I was just wondering what everyone thinks about #2 above - concurrent reads to the same container. I've assumed that in the past as well, but how safe do people think that is? If they add threading to the next standard, will they likely add #2 as a requirement on the standard containers? What I'm thinking about, for example, is a map-like container based on, say, splay trees, where the last accessed item gets moved towards the top of the tree, which is a nice property for caches. But this would mean mutable data that changes during reads. So I would have to protect that with a mutex or something? Thanks, Tony
Gottlob Frege wrote:
On 10/5/06, Joaquín Mª López Muñoz
wrote: No, it is not safe to insert from two threads into the same container. The thread safety guarantees aplying to Boost.MultiIndex (and to most STL implementations as well) are:
1. Concurrent access to different containers is safe. 2. Concurrent read-only access to the same container is safe
I know the standard says nothing about thread-safety (and I also know they are thinking about saying a few things next time), but I was just wondering what everyone thinks about #2 above - concurrent reads to the same container. I've assumed that in the past as well, but how safe do people think that is? If they add threading to the next standard, will they likely add #2 as a requirement on the standard containers?
It's very likely that #1+#2 will become the default level of thread safety for everything in C++. Of course one would be able to override this default by explicitly specifying in the documentation that a 'const' member function isn't really 'const' from thread safety point of view. But most, if not all, of the standard library will be #1+#2 (just like it offers basic exception safety by default, without it being explicitly documented on a function by function basis).
participants (4)
-
Gottlob Frege
-
Joaquín Mª López Muñoz
-
Peter Dimov
-
Prashant Thakre