QMutexLocker在鎖定線程時不工作。QMutexLocker未鎖定QMutex
答:
void foo() { // QMutexLocker(&mutex); // wrong, it will not just lock and unlock QMutexLocker locker(&mutex); // right, it will lock until the 'locker' scope ends ... }
QMutexLocker在鎖定線程時不工作。QMutexLocker未鎖定QMutex
答:
void foo() {
// QMutexLocker(&mutex); // wrong, it will not just lock and unlock
QMutexLocker locker(&mutex); // right, it will lock until the 'locker' scope ends
...
}
當然,這是錯誤的:
QMutexLocker(&mutex);
,因爲它會創建一個臨時的互斥,這樣的鎖,並在聲明的末尾解鎖。
這裏:
QMutexLocker locker(&mutex);
要創建QMutexLocker
類型的對象,名爲儲物櫃,其鎖定的互斥體,當它超出範圍將其解鎖。
第一個不是錯誤,它是完全有效的,雖然可能不是它的意圖:它會創建一個臨時鎖,它會鎖定互斥鎖。問題是,儲物櫃在聲明結束時立即被銷燬,並且互斥鎖被解鎖。 – 2012-03-07 20:41:38
@FrankOsterfeld謝謝。我嘗試了一些其他的東西(因爲在這個PC上我沒有安裝qt)並且失敗了,我認爲上面的代碼不能編譯。 – 2012-03-08 07:24:43
您對此有疑問嗎?看起來你已經明白了。 – 2012-03-07 18:27:34