Am I allowed to use the QMutex in the strange way: lock the QMutex in one thread, then unlock it in the other thread?
Asked
Active
Viewed 1,402 times
1
-
i guess if it is a shared object, it would be possible to hack around. but maybe it yould be easier to use a static boolean value and do the 'locking' manually – Zaiborg Sep 12 '13 at 07:41
-
What you're trying to achieve? – Kamil Klimek Sep 17 '13 at 08:05
2 Answers
2
According to the documentation of QMutex this results in an error:
Attempting to unlock a mutex in a different thread to the one that locked it results in an error.
But instead of a QMutex you can use a QSemaphore as a binary semaphore, to achieve the wanted behavior.
//create
QSemaphore semaphore(1);
//lock in thread 1
semaphore.acquire();
//unlock in thread 2
semaphore.release();

Istador
- 163
- 7
1
No, you can't do that:
Doc to QMutex::unlock()
"Unlocks the mutex. Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior."
Yes, it is indeed in shared memory, but there's no way to change it in a standard way.

Rift
- 86
- 5
-
Hi, please add link to the doc you used. It can be not allowed by documentation but may work in the real life, what kind of behavior is usual for Qt runinng in Linux? – osgx Sep 20 '13 at 10:32
-
if you use `QtCreator` you can look at the doc there ... it should be the doc for your version of qt. – Zaiborg Sep 23 '13 at 11:47