Re: http://qt.nokia.com/doc/4.0/threads.html
At the above URL we are given the following example:
Qt Code:
class Counter { public: Counter() { n = 0; } private: mutable QMutex mutex; int n; };To copy to clipboard, switch view to plain text mode
What makes this thread safe? I assume the QMutexLocker constructor is not atomic (I could be wrong). If two threads call increment, a QMutexLocker would be created. This will in part check to see that mutex is not currently locked. Is it not possible for both threads to call increment and both simultaneously begin to create a QMutexLocker, both QMutexLocker constructor calls simultaneously check the lock state of mutex, both simultaneously confirm that mutex is not currently locked, both lock mutex, both increment n, and then release. Ending up with the same non-thread safe issue you'd have if a mutex was not used at all.
Is a QMutexLocker more about lowering the chance of threading issues and not actually eliminating them? Perhaps I've overlooked something.
Bookmarks