abool

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 1 Imported by: 0

README

ABool 💡

Go Report Card GoDoc

Atomic Boolean library for Go, optimized for performance yet simple to use.

Use this for cleaner code.

Froked from github.com/tevino/abool.

Usage

import "github.com/barryz/abool"

cond := abool.New()  // default to false

cond.Set()                 // Set to true
cond.IsSet()               // Returns true
cond.UnSet()               // Set to false
cond.SetTo(true)           // Set to whatever you want
cond.SetToIf(false, true)  // Set to true if it is false, returns false(not set)
cond.Toggle() *AtomicBool  // Negates boolean atomically and returns a new AtomicBool object which holds previous boolean value.


// embedding
type Foo struct {
    cond *abool.AtomicBool  // always use pointer to avoid copy
}

Benchmark:

  • Go 1.14.2
  • OS X 10.15.5
# Read
BenchmarkMutexRead-12                   100000000               11.0 ns/op
BenchmarkAtomicValueRead-12             1000000000               0.253 ns/op
BenchmarkAtomicBoolRead-12              1000000000               0.259 ns/op    # <--- This package

# Write
BenchmarkMutexWrite-12                  100000000               10.8 ns/op
BenchmarkAtomicValueWrite-12            132855918                9.12 ns/op
BenchmarkAtomicBoolWrite-12             263941647                4.52 ns/op     # <--- This package

# CAS
BenchmarkMutexCAS-12                    54871387                21.6 ns/op
BenchmarkAtomicBoolCAS-12               267147930                4.50 ns/op     # <--- This package

# Toggle
BenchmarkMutexToggle-12                 55389297                21.4 ns/op
BenchmarkAtomicBoolToggle-12            145680895                8.32 ns/op     # <--- This package

Documentation

Overview

Package abool provides atomic Boolean type for cleaner code and better performance.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBool

type AtomicBool int32

AtomicBool is an atomic Boolean Its methods are all atomic, thus safe to be called by multiple goroutines simultaneously Note: When embedding into a struct, one should always use *AtomicBool to avoid copy

Example
cond := New()    // default to false
cond.Set()       // set to true
cond.IsSet()     // returns true
cond.UnSet()     // set to false
cond.SetTo(true) // set to whatever you want
cond.Toggle()    // toggles the boolean value
Output:

func New

func New() *AtomicBool

New creates an AtomicBool with default to false

func NewBool

func NewBool(ok bool) *AtomicBool

NewBool creates an AtomicBool with given default value

func (*AtomicBool) IsSet

func (ab *AtomicBool) IsSet() bool

IsSet returns whether the Boolean is true

func (*AtomicBool) Set

func (ab *AtomicBool) Set()

Set sets the Boolean to true

func (*AtomicBool) SetTo

func (ab *AtomicBool) SetTo(yes bool)

SetTo sets the boolean with given Boolean.

func (*AtomicBool) SetToIf

func (ab *AtomicBool) SetToIf(old, new bool) (set bool)

SetToIf sets the Boolean to new only if the Boolean matches the old Returns whether the set was done

func (*AtomicBool) Toggle

func (ab *AtomicBool) Toggle() bool

Toggle negates boolean atomically and returns the previous boolean value.

func (*AtomicBool) UnSet

func (ab *AtomicBool) UnSet()

UnSet sets the Boolean to false

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL