2 Star 2 Fork 8

王布衣/gox

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
channel.go 514 Bytes
Copy Edit Raw Blame History
王布衣 authored 2024-01-25 12:55 +08:00 . 优化精度条存在race的问题
package coroutine
import (
"sync"
"sync/atomic"
)
// Channel 多协程安全的channel
type Channel[E any] struct {
data chan E
m sync.Mutex
num atomic.Int64
done atomic.Uint32
}
func (c *Channel[E]) Push(v E) {
if c.done.Load() == 1 {
return
}
c.data <- v
c.num.Add(1)
}
func (c *Channel[E]) Pop() E {
v := <-c.data
c.num.Add(-1)
return v
}
func (c *Channel[E]) Close() {
if c.done.Load() != 0 {
return
}
c.m.Lock()
defer c.m.Unlock()
if c.done.Load() == 0 {
c.done.Store(1)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.22.9

Search