英文:
Why different array length output in concurrent golang programming?
问题
我正在使用golang编写一个简单的并发测试程序,并且不理解每次运行时capitalized
数组大小的不同输出!
data := []rune{'a', 'b', 'c', 'd'}
var capitalized []rune
capIt := func(r rune) {
capitalized = append(capitalized, unicode.ToUpper(r))
fmt.Printf("%c done!\n", r)
}
fmt.Printf("Before: %c\n", capitalized)
for i := 0; i < len(data); i++ {
go capIt(data[i])
}
time.Sleep(100 * time.Millisecond)
fmt.Printf("After: %c\n", capitalized)
输出结果:
b done! a done! d done! c done! After: [D B C A]
a done! d done! c done! b done! After: [D B A]
d done! a done! c done! b done! After: [B]
d done! a done! c done! b done! After: [A B C]
d done! b done! a done! c done! After: [B C]
英文:
I'm writing a simple program in golang for concurrency test and don't understand different output of capitalized
array size every time!
data := []rune{'a', 'b', 'c', 'd'}
var capitalized []rune
capIt := func(r rune) {
capitalized = append(capitalized, unicode.ToUpper(r))
fmt.Printf("%c done!\n", r)
}
fmt.Printf("Before: %c\n", capitalized)
for i := 0; i < len(data); i++ {
go capIt(data[i])
}
time.Sleep(100 * time.Millisecond)
fmt.Printf("After: %c\n", capitalized)
Outputs:
> b done! a done! d done! c done! After: [D B C A]
> a done! d done! c done! b done! After: [D B A]
> d done! a done! c done! b done! After: [B]
> d done! a done! c done! b done! After: [A B C]
> d done! b done! a done! c done! After: [B C]
答案1
得分: 2
你有数据竞争。
$ go run -race racer.go
Before: []
=================
警告:数据竞争
由 goroutine 8 写入 0x00c000012018:
main.main.func1()
racer.go:14 +0xc4
main.main.func2()
racer.go:20 +0x3e
由 goroutine 7 之前读取 0x00c000012018:
main.main.func1()
racer.go:14 +0x44
main.main.func2()
racer.go:20 +0x3e
Goroutine 8(正在运行)创建于:
main.main()
racer.go:20 +0x199
Goroutine 7(正在运行)创建于:
main.main()
racer.go:20 +0x199
=================
a 完成!
=================
警告:数据竞争
由 goroutine 9 写入 0x00c000012018:
main.main.func1()
racer.go:14 +0xc4
main.main.func2()
racer.go:20 +0x3e
由 goroutine 10 之前写入 0x00c000012018:
main.main.func1()
racer.go:14 +0xc4
main.main.func2()
racer.go:20 +0x3e
Goroutine 9(正在运行)创建于:
main.main()
racer.go:20 +0x199
Goroutine 10(正在运行)创建于:
main.main()
racer.go:20 +0x199
=================
d 完成!
b 完成!
c 完成!
After: [B C]
发现 2 个数据竞争
退出状态 66
$
racer.go:
package main
import (
"fmt"
"time"
"unicode"
)
func main() {
data := []rune{'a', 'b', 'c', 'd'}
var capitalized []rune
capIt := func(r rune) {
capitalized = append(capitalized, unicode.ToUpper(r))
fmt.Printf("%c 完成!\n", r)
}
fmt.Printf("Before: %c\n", capitalized)
for i := 0; i < len(data); i++ {
go capIt(data[i])
}
time.Sleep(100 * time.Millisecond)
fmt.Printf("After: %c\n", capitalized)
}
英文:
You have data races.
$ go run -race racer.go
Before: []
==================
WARNING: DATA RACE
Write at 0x00c000012018 by goroutine 8:
main.main.func1()
racer.go:14 +0xc4
main.main.func2()
racer.go:20 +0x3e
Previous read at 0x00c000012018 by goroutine 7:
main.main.func1()
racer.go:14 +0x44
main.main.func2()
racer.go:20 +0x3e
Goroutine 8 (running) created at:
main.main()
racer.go:20 +0x199
Goroutine 7 (running) created at:
main.main()
racer.go:20 +0x199
==================
a done!
==================
WARNING: DATA RACE
Write at 0x00c000012018 by goroutine 9:
main.main.func1()
racer.go:14 +0xc4
main.main.func2()
racer.go:20 +0x3e
Previous write at 0x00c000012018 by goroutine 10:
main.main.func1()
racer.go:14 +0xc4
main.main.func2()
racer.go:20 +0x3e
Goroutine 9 (running) created at:
main.main()
racer.go:20 +0x199
Goroutine 10 (running) created at:
main.main()
racer.go:20 +0x199
==================
d done!
b done!
c done!
After: [B C]
Found 2 data race(s)
exit status 66
$
racer.go:
package main
import (
"fmt"
"time"
"unicode"
)
func main() {
data := []rune{'a', 'b', 'c', 'd'}
var capitalized []rune
capIt := func(r rune) {
capitalized = append(capitalized, unicode.ToUpper(r))
fmt.Printf("%c done!\n", r)
}
fmt.Printf("Before: %c\n", capitalized)
for i := 0; i < len(data); i++ {
go capIt(data[i])
}
time.Sleep(100 * time.Millisecond)
fmt.Printf("After: %c\n", capitalized)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论