On Friday 22 January 2016 19:37:22 Matthew Woehlke wrote: > On 2016-01-22 13:02, Bubke Marco wrote: > > Actually what is happen if I am in the middle of changing a qvector, and > > then copying the vector in an other thread? > > Are you concurrently modifying and copying *the same* vector, or a > shallow copy that, under the hood, happens to be shared? The latter is > no problem; as soon as you try to modify the vector, it will detach, and > they'll cease being shared. The one that's just being copied is not > modified. > > If you really have *the same* vector... Don't do that :-). Note that > this implies that you are either operating on the same variable, or one > thread is operating on a reference. That's not very common, and it's > just not thread safe, ever¹, regardless of the data type. > > (¹ Well, besides atomic types, but we're talking about containers...)
Actually, now that you mention this, std::vector guarantees that concurrent write access to non-overlapping regions of a single vector is ok: std::vector<QString> strings = ...; auto process = [&strings](int from, int to) { for (int i = from; i < to; ++i) strings[i] = strings[i].toLower(); } auto t = std::thread(process, 0, strings.size() / 2); process(string.size() / 2, string.size()); t1.join(); For QVector, you need to ensure the container is detached to start with. If it is shared, two threads may try to detach at the same time, racing over who gets to update the d pointer. Granted, this is easily fixed by handing iterators to the lambda, but only because that causes the QVector to detach before any thread is started. I expect this to bite when people try to use #pragma omp parallel or similar. I don't think omp parallel loops can use iterators as control variables. They need indexes. Thanks, Marc -- Marc Mutz <marc.m...@kdab.com> | Senior Software Engineer KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company Tel: +49-30-521325470 KDAB - The Qt Experts _______________________________________________ Development mailing list Development@qt-project.org http://lists.qt-project.org/mailman/listinfo/development