2 回答

TA貢獻1862條經驗 獲得超7個贊
沒有互斥鎖。當代碼執行時解鎖():
if currentNode == nil {
fmt.Println("There are no patients.")
return nil
}
您正在鎖定,但從未解鎖:
func (p *queue) displayallpatients() error {
defer wg.Done()
mutex.Lock() // <- here we acquire a lock
{
currentNode := p.front
if currentNode == nil {
fmt.Println("There are no patients.")
return nil // <- here we return without releasing the lock
}
// ...
}
mutex.Unlock() // <- never reach if currentNode == nil is true
return nil
}
您可以使用延遲或不要進行提前返回來解決此問題:
func (p *queue) displayallpatients() error {
defer wg.Done()
defer mutex.Unlock() // <- defers the execution until the func returns (will release the lock)
mutex.Lock()
{
currentNode := p.front
if currentNode == nil {
fmt.Println("There are no patients.")
return nil
}
// ...
}
return nil
}
您可以在文檔中找到更多詳細信息
- 2 回答
- 0 關注
- 90 瀏覽
添加回答
舉報