This repository demonstrates how to atomically load and store a pointer value in Go using the sync/atomic
package. It showcases safe concurrent updates to pointer values, ensuring data integrity across goroutines.
- This example covers the use of atomic operations to load and store a pointer value in a thread-safe manner.
- It demonstrates the use of `atomic.Value` for storing and retrieving pointer values concurrently between goroutines.
- The example includes updating the value in a separate goroutine and safely retrieving it using atomic operations.
package main
import (
"fmt"
"sync/atomic"
)
func main() {
// Atomic Pointer
// demonstrate how to atomically load and store a pointer value
var data atomic.Value
// Store an initial value
data.Store("Initial Value")
// Load and print the current value
fmt.Println("Current Value:", data.Load())
// Start a goroutine to change the value
go func() {
data.Store("New Value")
}()
// Load and print the new value
fmt.Println("New Value:", data.Load())
}
- Make sure you have Go installed. If not, you can download it from here.
- Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
- Navigate to the 025_atomic_counters/005_atomic_pointer directory:
cd go_sample_examples/025_atomic_counters/005_atomic_pointer
- Run the Go program:
go run main.go
When you run the program, you should see an output similar to:
Current Value: Initial Value
New Value: New Value