Yeh*_*tan 1 concurrency go goroutine
出于某种原因,当我删除fmt.Printlns然后代码阻塞.我不知道为什么会这样.我想要做的就是实现一个简单的并发限制器......
我从来没有经历过这么奇怪的事情.这就像fmt冲刷变量或其他东西并使其工作.
此外,当我使用常规函数而不是goroutine时,它也可以工作.
这是以下代码 -
package main
import "fmt"
type ConcurrencyLimit struct {
active int
Limit int
}
func (c *ConcurrencyLimit) Block() {
for {
fmt.Println(c.active, c.Limit)
// If should block
if c.active == c.Limit {
continue
}
c.active++
break
}
}
func (c *ConcurrencyLimit) Decrease() int {
fmt.Println("decrease")
if c.active > 0 {
c.active--
}
return c.active
}
func main() {
c := ConcurrencyLimit{Limit: 1}
c.Block()
go func() {
c.Decrease()
}()
c.Block()
}
Run Code Online (Sandbox Code Playgroud)