atomicbool

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 1 Imported by: 0

README

Golang AtomicBool

Test GoDoc

An atomic boolean type that can be safely shared between goroutines. It uses atomic loads and stores of int32.

Getting Started

import atomicbool "github.com/yaa110/atomic-bool"

ab := atomicbool.New(false)
ab.Load() // returns false
ab.Store(true) // returns false (old value)
ab.Load() // returns true

Use Cases

Idempotent Close of a Closer
type ClosableType struct {
    closed *atomicbool.AtomicBool // atomicbool.New(false)
}

func (c *ClosableType) Close() error {
    if c.closed.Load() || c.closed.Store(true) {
        return nil
    }

    // TODO close
}
Run if it is not already Running
type RunnableType struct {
    running *atomicbool.AtomicBool // atomicbool.New(false)
}

func (r *RunnableType) Run() error {
    if r.running.Load() || r.running.Store(true) {
        return nil
    }

    defer r.running.Store(false)
    // TODO run
}

Documentation

Overview

Package atomicbool provides an atomic boolean type that can be safely shared between goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBool

type AtomicBool struct {
	// contains filtered or unexported fields
}

func New

func New(state bool) *AtomicBool

New creates a new instance of `AtomicBool` with the initial value of `state`.

func (*AtomicBool) Load

func (a *AtomicBool) Load() bool

Load loads the value.

func (*AtomicBool) Store

func (a *AtomicBool) Store(state bool) bool

Store stores `state` and returns old value.

Jump to

Keyboard shortcuts

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