If you’ve looked into source codes, you will often come across term like mutex. This post unravels its purpose and advantages.
There have been many issues to multi thread programming. The most prominent one being the atomicity (An illusion that a section of code either executes completely, or doesn’t execute at all).
A mutex meets the atomicity requirement. Officially: “Mutex are typically used to serialize access to a section of code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.”
You might be amazed to know that we have a class in Qt, known as QMutex, that provides access serialization between threads. QMutex is optimized to be fast. It is constructed and destroyed with almost no overhead, which means it is fine to have many mutexes as part of other classes.
QMutexLocker is like an icing on the cake. It’s best to use both as this makes it easy to ensure that locking and unlocking are performed consistently.
Many software use mutex protection to manage multi threads. One such is digiKam, an advanced digital photo management application for Linux, Windows, and Mac-OS X which manages it’s huge source code using QMutex class.
For more advanced information, take a tour of official Qt Documentation!