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

**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)


小怪兽爱吃肉
浏览 61回答 2
2回答

牧羊人nacy

没有互斥锁。当代码执行时解锁():if currentNode == nil {&nbsp; &nbsp; fmt.Println("There are no patients.")&nbsp; &nbsp; return nil}您正在锁定,但从未解锁:func (p *queue) displayallpatients() error {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; mutex.Lock() // <- here we acquire a lock&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; currentNode := p.front&nbsp; &nbsp; &nbsp; &nbsp; if currentNode == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("There are no patients.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil // <- here we return without releasing the lock&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }&nbsp; &nbsp; mutex.Unlock() // <- never reach if currentNode == nil is true&nbsp; &nbsp; return nil}您可以使用延迟或不要进行提前返回来解决此问题:func (p *queue) displayallpatients() error {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; defer mutex.Unlock() // <- defers the execution until the func returns (will release the lock)&nbsp; &nbsp; mutex.Lock()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; currentNode := p.front&nbsp; &nbsp; &nbsp; &nbsp; if currentNode == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("There are no patients.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return nil}您可以在文档中找到更多详细信息

至尊宝的传说

在开始日常使用之前使用。wg.Add(1)
打开App,查看更多内容
随时随地看视频慕课网APP