为了账号安全,请及时绑定邮箱和手机立即绑定

即使使用互斥锁和延迟,也会对运行 goroutine 感到恐慌

即使使用互斥锁和延迟,也会对运行 goroutine 感到恐慌

Go
小怪兽爱吃肉 2022-08-24 15:48:19
**func (p *linkedList) showalldoctorsrecursive() error {    defer wg.Done()    mutex.Lock()    {        if p.head != nil {            printRecursiveF(p.head)        } else {            fmt.Println("The list is empty.")        }    }    mutex.Unlock()    return nil}func (p *linkedList) showalldoctorsfront() error {    defer wg.Done()    mutex.Lock()    {        if p.head != nil {            printfront(p.head)        } else {            fmt.Println("The list is empty.")        }    }    mutex.Unlock()    return nil}func printRecursiveF(n *Node2) {    if n != nil {        printRecursiveF(n.next)        fmt.Println(n.item, n.next.time)    }}func printfront(n *Node2) {    if n != nil {        fmt.Println(n.item)        fmt.Println(n.item, n.next.time)    }}**func (p *queue) displayallpatients() error {    defer wg.Done()    mutex.Lock()    {        currentNode := p.front        if currentNode == nil {            fmt.Println("There are no patients.")            return nil        }        fmt.Println("Displaying all patients and their appointment times")        fmt.Println(currentNode.item, currentNode.time)        for currentNode.next != nil {            currentNode = currentNode.next            fmt.Println(currentNode.item, currentNode.time)        }    }    mutex.Unlock()    return nil}func main() {    var num, num1, num2 int    runtime.GOMAXPROCS(2)    wg.Add(3)    myList := &linkedList{nil, 0}    myQueue := &queue{nil, nil, 0}    for {        fmt.Println("Please enter 1 to check for patient or 2 to check for doctor.Alternatively,enter 3 to exit menu")        fmt.Scanln(&num)        _, err := mainmenu(num)        if err != nil {            fmt.Println(err)        } else if num == 3 {            break        } else {            fmt.Printf("Please proceed to the main menu >")        }我尝试运行 goroutines 并应用互斥锁和延迟。但是,在运行goroutines时,我将面临恐慌。有没有办法解决这个问题?我创建了一个队列和链表,其中包含一些要显示的函数,enqueue(add)和dequeue(pop),并为此应用了一些并发性。我知道这一点,n是你运行的goroutine的数量,你想使用互斥锁来确保goroutines一次运行一个。wg.Add(n)
查看完整描述

2 回答

?
牧羊人nacy

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

}

您可以在文档中找到更多详细信息


查看完整回答
反对 回复 2022-08-24
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

在开始日常使用之前使用。wg.Add(1)



查看完整回答
反对 回复 2022-08-24
  • 2 回答
  • 0 关注
  • 60 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信