Version: v0.2.3 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2015 License: MIT Imports: 1 Imported by: 0




This section is empty.


This section is empty.


This section is empty.


type Counter

type Counter interface {
	// Incrememnt adds a number of todos to track.
	// If the counter is **below** zero, it panics.
	Increment(i uint32)

	// Decrement removes a number of todos to track.
	// If the count drops to zero, signals done and destroys the counter.
	// If the count drops **below** zero, panics. It means you have tried to remove
	// more things than you added, i.e. sync issues.
	Decrement(i uint32)

	// Done returns a channel to wait upon. Use it in selects:
	//  select {
	//  case <-ctr.Done():
	//    // done processing all items
	//  }
	Done() <-chan struct{}

Counter records things remaining to process. It is needed for complicated cases where multiple goroutines are spawned to process items, and they may generate more items to process. For example, say a query over a set of nodes may yield either a result value, or more nodes to query. Signaling is subtly complicated, because the queue may be empty while items are being processed, that will end up adding more items to the queue.

Use Counter like this:

todos := make(chan int, 10)
ctr := todoctr.NewCounter()

process := func(item int) {
  fmt.Println("processing %d\n...", item)

  // this task may randomly generate more tasks
  if rand.Intn(5) == 0 {
    todos<- item + 1
    ctr.Increment(1) // increment counter for new task.

  ctr.Decrement(1) // decrement one to signal the task being done.

// add some tasks.
todos<- 1
todos<- 2
todos<- 3
todos<- 4

for {
  select {
  case item := <- todos:
    go process(item)
  case <-ctr.Done():
    fmt.Println("done processing everything.")

func NewSyncCounter

func NewSyncCounter() Counter

NewSyncCounter constructs a new counter

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL