Consider this: -
char buffer[]
void ReadFromBuffer()
{
QMutexLocker ml(&m_Mutex);
// read contents of buffer
}
void WriteToBuffer()
{
QMutexLocker ml(&m_Mutex);
// write to buffer
}
If ReadFromBuffer is running in a separate thread to WriteToBuffer, you want to ensure that read and write don't occur at the same time.
If one thread is executing in WriteToBuffer and locks the mutex, when the other thread enters ReadFromBuffer and executes its mutex locker, it checks to see if the mutex is locked. If it is locked, the thread pauses and waits for it to be unlocked. This occurs when the QMutexLocker object goes out of scope in the first function that locked the mutex.
That's why QMutexLocker is required around all code accessing shared objects.