1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package queue
- import (
- "sync"
- "time"
- )
- // buffered golang chan members release
- type Clear struct {
- flag bool
- lock sync.RWMutex
- }
- // Clean() begin and wait for remaining chan members were released
- func (i *Clear) Clean(queue chan interface{}) {
- i.set(true)
- for {
- if len(queue) == 0 {
- i.set(false)
- break
- }
- time.Sleep(1 * time.Millisecond) // set a little gap between every loop and between Clean() and clear.Working() off
- }
- }
- // set() set the Clear working flag
- func (i *Clear) set(val bool) {
- i.lock.Lock()
- defer i.lock.Unlock()
- i.flag = val
- }
- // Working() return whether the Clear is working
- func (i *Clear) Working() bool {
- i.lock.RLock()
- defer i.lock.RUnlock()
- return i.flag
- }
|