github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/counter/counter.go (about)

     1  package counter
     2  
     3  import "sync"
     4  
     5  // MutexCounter is a thread-safe counter
     6  type MutexCounter struct {
     7  	i int
     8  	m sync.Mutex
     9  }
    10  
    11  // Add increments the counter by 1
    12  func (mc *MutexCounter) Add() int {
    13  	mc.m.Lock()
    14  	mc.i++
    15  	i := mc.i
    16  	mc.m.Unlock()
    17  
    18  	return i
    19  }
    20  
    21  // Set redefines the counter to a specified value
    22  func (mc *MutexCounter) Set(i int) {
    23  	mc.m.Lock()
    24  	mc.i = i
    25  	mc.m.Unlock()
    26  }
    27  
    28  // NotEqual tests if the counters value is the same as the supplied value
    29  func (mc *MutexCounter) NotEqual(i int) bool {
    30  	mc.m.Lock()
    31  	ne := mc.i != i
    32  	mc.m.Unlock()
    33  	return ne
    34  }