I was wondering about how to use mutex for multithreaded application. Do my getters need to lock too? I found this post. The answer is yes, I need to guard getters functions
but that means I’ll be able to perform one read at a time, and I wonder if it could be improved.
T.E.D’s answer suggests that you can implement locks in a way that many threads can read the data at the same time if setters’ functions didnt lock the mutex to perform a write.
I tried to find some examples – reading Qt’s documentation – tought, QMutex doesn’t have a isLocked() function or something like this. So how can you praticly implements this kind of “intelligent’s locks’.
Thanks
You need a special kind of locking mechanism called readers-writer lock. With this lock any number of readers can access the resource simultaneously, but for a writer to have access all reader threads must block.
Looks like Qt has a QReadWriteLock class that implements this mechanism.