abool

package
v5.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2024 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Overview

Package abool provides atomic Boolean type for cleaner code and better performance. All rights reserved to https://github.com/tevino/abool.

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()                // Sets to true
cond.IsSet()              // Returns true
cond.UnSet()              // Sets to false
cond.IsNotSet()           // Returns true
cond.SetTo(true)          // Sets to whatever you want
cond.SetToIf(true, false) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
cond.Toggle()             // Inverts the boolean then returns the value before inverting
Output:

func New

func New() *AtomicBool

New creates an AtomicBool with default set to false.

func NewBool

func NewBool(ok bool) *AtomicBool

NewBool creates an AtomicBool with given default value.

func (*AtomicBool) IsNotSet

func (ab *AtomicBool) IsNotSet() bool

IsNotSet returns whether the Boolean is false.

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, curr 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 inverts the Boolean then returns the value before inverting.

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