我的代码是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
谢谢,它工作正常 –