How it works...

Qt 5 provides two classes, namely QMutex and QReadWriteLock, for data protection when multiple threads are accessing and modifying the same data. We only used QMutex in the previous example, but both classes are very similar in nature. The only difference is that QReadWriteLock allows data to be read simultaneously by other threads while the data is being written. Unlike QMutex, it separates the read and write states, but only one can occur at a time (either lock for read, or lock for write), and not both. For complex functions and statements, use the high-level QMutexLocker class instead of QMutex for simplifying the code and easier debugging.

The downside of this method is that all of the other threads will be idling around while the data is being modified by a single thread. It is best not to share data with multiple threads unless there is no other way to do so, as it will halt the other threads and defeat the object of parallel computation.