문제

Am I allowed to use the QMutex in the strange way: lock the QMutex in one thread, then unlock it in the other thread?

도움이 되었습니까?

해결책

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();

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top