@deisik said in Is QList thread-safe for certain operations?:
So is QList thread-safe for modifying one of its elements (with replace()) from one thread while reading its other elements (with at()) from another?
This isn't entirely about thread-safety of QList anymore. If you are storing ints inside the QList, at() will retrieve either the old value or the new value (assuming the ints are properly aligned) when do a replace() at the same time on the same index. If you store your own class in the container, your class would have to be somewhat thread-safe as well. However, I don't know an easy mechanism for locking assignment to the object (i.e. replacing its contents) against any access to the object while it is being changed. (Unless you synchronize all access to the object with a mutex.) If you store pointers to objects inside your container you get entirely different problems. Replacing a pointer is as safe as replacing an int. But, you might have a local copy of a pointer to the object which will now be replaced inside the QList. Most likely, the object will then also be destroyed by the thread that replaced the object. The local copy of the pointer is now invalid. Using shared pointers might help in this context, but they will slow down every access to the objects inside your container. (And you need to make sure that you get a copy of the shared pointer. The default implementation of at() will return a reference to whatever is stored inside the container, not a copy.)