queue_clear.go 712 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package queue
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // buffered golang chan members release
  7. type Clear struct {
  8. flag bool
  9. lock sync.RWMutex
  10. }
  11. // Clean() begin and wait for remaining chan members were released
  12. func (i *Clear) Clean(queue chan interface{}) {
  13. i.set(true)
  14. for {
  15. if len(queue) == 0 {
  16. i.set(false)
  17. break
  18. }
  19. time.Sleep(1 * time.Millisecond) // set a little gap between every loop and between Clean() and clear.Working() off
  20. }
  21. }
  22. // set() set the Clear working flag
  23. func (i *Clear) set(val bool) {
  24. i.lock.Lock()
  25. defer i.lock.Unlock()
  26. i.flag = val
  27. }
  28. // Working() return whether the Clear is working
  29. func (i *Clear) Working() bool {
  30. i.lock.RLock()
  31. defer i.lock.RUnlock()
  32. return i.flag
  33. }