liveBook · Manning

LoadInt32

As explained in Learn Concurrent Programming with Go

LoadInt32 is a Go sync/atomic function that atomically loads a signed 32-bit integer value.

LoadInt32 atomically reads a signed 32-bit integer through Go's sync/atomic package. It provides a safe read of a shared atomic value after concurrent goroutines finish. section 12.1.1 section 12.1.3

Reading a shared balance

After Stingy and Spendy update the shared int32 with atomic additions, a WaitGroup waits for both goroutines, then atomic.LoadInt32(&money) reads the final balance. The program reports $100 without race conditions. section 12.1.1

Use LoadInt32 when reading results produced by atomic updates:

  • Store shared counters as int32 values because these atomic operations apply to specific integer types. section 12.1.3
  • Load each completed counter with atomic.LoadInt32, even when goroutines have already finished, to follow atomic-read practice. section 12.1.3
func AddInt32(addr *int32, delta int32) (new int32)
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
func LoadInt32(addr *int32) (val int32)
func StoreInt32(addr *int32, val int32)
func SwapInt32(addr *int32, new int32) (old int32)

View LoadInt32 alongside Go's related atomic operations and function signatures. section 12.1.1

How Learn Concurrent Programming with Go covers it

How deep it goes
Defines the call and demonstrates it in completed-goroutine examples, without descending into atomic implementation internals.
Examples
Demonstrates final money balances and per-letter frequency counts collected from multiple web documents.
What it assumes
Uses shared int32 variables, goroutines, atomic updates, and WaitGroup completion before demonstrating the read.
What you can do after
Apply it via Go's sync/atomic package to read shared int32 values after concurrent work.
How it connects
Connects atomic loads to atomic updates, shared variables, goroutines, and WaitGroup synchronization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.