我的代碼是AVL樹,我試圖用mutex
輸入。互斥鎖不起作用
mutex
不起作用。爲什麼?
返回黑屏,可能是死鎖。 我不知道。 不存在遞歸函數。
如果我使用lock guard
它可以正常工作。
template<typename T> int avl<T>::insere (int key , T data) {
mtx.lock();
no * nw = new no;
if (nw == NULL)
return 0;
nw->sire = NULL;
nw->left = NULL;
nw->right = NULL;
nw->key = key;
nw->data = data;
if (tree.root == NULL) {
tree.root = nw;
tree.quant++;
return 1;
}
no * son = tree.raiz;
no * sire = NULL;
while (son != NULL) {
sire = son;
if (key < son->key)
son = son->left;
else
son = son->right.;
}
nw->sire = sire;
if (key < sire->key)
sire->left = nw;
else
sire->right = nw;
tree.quantidade++;
no * current = nw;
while (current != NULL) {
int f = fator (nw);
if (f >= 2 || f <= 2)
balance(current);
current = current->sire;
}
mtx.unlock();
return 1;
}
你的函數中有多個返回語句,你不解鎖你的互斥鎖。 lock_guard會自動爲您執行此操作。如果你不使用它,你必須在每次返回之前解鎖互斥鎖 – Hayt
謝謝,它工作正常 –