gpt4 book ai didi

go - sync.WaitGroup.Done()实际如何工作?

转载 作者:行者123 更新时间:2023-12-01 22:32:19 25 4
gpt4 key购买 nike

我为创建4个goroutine的程序运行了以下代码,根据输出我得到了我的代码似乎正常运行但出现错误的信息:-fatal error: all goroutines are asleep - deadlock!我添加了4个WaitGroup,在完成每个goroutine时,我都做了wg.Done(),但我仍然不明白为什么会发生这种错误。
我的代码:

package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func sort(x []int) {

fmt.Println("Initial: ", x)

var i int
var j int
for i = 0; i < len(x); i++ {
for j = 0; j < len(x)-i-1; j++ {
if x[j] > x[j+1] {
temp := x[j+1]
x[j+1] = x[j]
x[j] = temp
}
}
}

fmt.Println("Sorted: ", x)

wg.Done()

}

func main() {

fmt.Println("Enter an array of number preferrably in multiples of 4:- ")
inputSlice := make([]int, 0, 1)

for true {
var n int
fmt.Print("Enter the number: ")
fmt.Scan(&n)

inputSlice = append(inputSlice, n)
fmt.Print("Do you want to continue adding numbers (y/n) ? ")
var char string
fmt.Scan(&char)

if char == "n" || char == "N" {
if len(inputSlice)%4 == 0 {
break
} else {
fmt.Println("Please enter more ", 4-len(inputSlice)%4, " numbers")
}
}
fmt.Println("Test: ", n)
fmt.Println("Input: ", char)
}

fmt.Println("Initial: ", inputSlice)
size := len(inputSlice)

wg.Add(4)

div := (size / 4)
sort1 := inputSlice[:div]
go sort(sort1)

sort2 := inputSlice[div:(div * 2)]
go sort(sort2)

sort3 := inputSlice[(div * 2):(div * 3)]
go sort(sort3)

sort4 := inputSlice[(div * 3):]
go sort(sort4)

wg.Wait()

final := make([]int, 0, 1)

for _, val := range sort1 {
final = append(final, val)
}
for _, val := range sort2 {
final = append(final, val)
}
for _, val := range sort3 {
final = append(final, val)
}
for _, val := range sort4 {
final = append(final, val)
}

sort(final)

fmt.Println("Sorted: ", final)

}
我的输出:
Enter an array of number preferrably in multiples of 4:- 
Enter the number: 1
Do you want to continue adding numbers (y/n) ? y
Test: 1
Input: y
Enter the number: 2
Do you want to continue adding numbers (y/n) ? y
Test: 2
Input: y
Enter the number: 3
Do you want to continue adding numbers (y/n) ? y
Test: 3
Input: y
Enter the number: 4
Do you want to continue adding numbers (y/n) ? n
Initial: [1 2 3 4]
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.main()
C:/Deepan/Text Books/NITK - Coursera/GoLang/Course3/week3.go:61 +0x658
exit status 2

最佳答案

实际上,我没有遇到死锁,但遇到了另一个错误:panic: sync: negative WaitGroup counter
您将4添加到等待组:

wg.Add(4)
然后在4个启动的goroutines中的 wg.Done()中调用 sort()。没关系。
但您在 sort()中也有一个“最终” main调用:
sort(final)
wg.Done()内部也将被称为:
panic: sync: negative WaitGroup counter
一个“简单”的解决方法是在调用 sort之前将一个添加到等待组:
wg.Add(1)
sort(final)

关于go - sync.WaitGroup.Done()实际如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62832004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com