// 新的替换旧的
type FlashFunc struct {
b atomic.Uintptr
+ l sync.Mutex
c *context.CancelFunc
}
func (t *FlashFunc) Flash() (current uintptr) {
current = uintptr(unsafe.Pointer(&struct{}{}))
t.b.Store(current)
- if t.c != nil {
- (*t.c)()
- t.c = nil
- }
return
}
return t.b.Load() != current
}
-func (t *FlashFunc) FlashWithContext() (current uintptr, c context.Context) {
- current = uintptr(unsafe.Pointer(&struct{}{}))
- t.b.Store(current)
+func (t *FlashFunc) FlashWithContext() (c context.Context) {
+ t.l.Lock()
+ defer t.l.Unlock()
+
if t.c != nil {
(*t.c)()
t.c = nil
var cc = make(chan int, 2)
var b FlashFunc
var a = func(i int) {
- _, c := b.FlashWithContext()
+ c := b.FlashWithContext()
<-c.Done()
cc <- i
}