atomicbool

package module
v0.0.0-...-e8be8f0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2019 License: BSD-3-Clause Imports: 1 Imported by: 1

README

go.atomicbool

Build Status GoDoc Coverage Status Go Report Card

An atomic Go boolean type with function interfaces like official sync/atomic.

Example:

Playground

package main

import (
	"fmt"
	"github.com/Andrew-M-C/go.atomicbool"
)

func main() {
	f := fmt.Println

	b := atomicbool.New(true)
	f(b.Load())

	b.Store(false)
	f(b.Load())

	swapped := b.CompareAndSwap(false, true)
	f(swapped, b.Load())
}

Documentation

Overview

Package atomicbool provides a type with function interfaces like official sync/atomic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type B

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

B is the replacement of bool in atomic scenarios. It is OK to initialize an atomicbool.B with b := atomicbool.B{}

Example
package main

import (
	"fmt"

	atomicbool "github.com/Andrew-M-C/go.atomicbool"
)

func main() {
	f := fmt.Println

	b := atomicbool.New(true)
	f(b.Load())

	b.Store(false)
	f(b.Load())

	swapped := b.CompareAndSwap(false, true)
	f(swapped, b.Load())
}
Output:

true
false
true true

func New

func New(b bool) *B

New returns an atomicbool value with specified boolean value. It is useful when you want to initialize it as true or values by other channel. Otherwise, just use b := atomicbool.B{}.

func (*B) CompareAndSwap

func (a *B) CompareAndSwap(old, new bool) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an atomicbool.

func (*B) Load

func (a *B) Load() bool

Load read the boolean value thread-safelly.

func (*B) Store

func (a *B) Store(b bool)

Store set the boolean value thread-safelly.

Jump to

Keyboard shortcuts

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