亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

即使使用互斥鎖和延遲,也會對運行 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 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號