github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xsync/xatomic/README.md (about)

     1  # XAtomic
     2  标准库中提供了 `int32` , `int64` , `uint32` , `uint64` 的原子量,却没有提供 `float32` , `float64` , `string` , `bool` 的原子量,因此,在这里,提供了比标准库更为丰富的原子量
     3  
     4  ## AtomicBool
     5  
     6  定义
     7  
     8  ``` go
     9  package xatomic
    10  type AtomicBool struct{ v uint32 }
    11  ```
    12  
    13  使用
    14  
    15  ``` go
    16  package main
    17  import (
    18      "github.com/go-board/x-go/xsync/xatomic"
    19  )
    20  
    21  func main() {
    22      b := xatomic.NewAtomicBool(false)
    23      println(b.Load()) // false
    24      println(b.CAS(false, true)) // true
    25      b.Store(true)
    26      println(b.Swap(false)) // true
    27      println(b.Toggle()) // true
    28  }
    29  ```
    30  
    31  ## AtomicFloat32/64
    32  
    33  定义
    34  
    35  ``` go
    36  package xatomic
    37  type AtomicFloat64 struct {	v uint64 }
    38  type AtomicFloat32 struct { v uint32 }
    39  ```
    40  
    41  使用
    42  
    43  ``` go
    44  package main
    45  
    46  import (
    47      "github.com/go-board/x-go/xsync/xatomic"
    48  )
    49  
    50  func testAtomicFloat32() {
    51      f := xatomic.NewAtomicFloat32(0.01)
    52      println(f.Load()) // 0.01
    53      f.Store(2.001)
    54      println(f.Add(0.01)) // 2.011
    55      println(f.Sub(0.01)) // 2.001
    56      println(f.CAS(2.001, 3)) // 2.001
    57  }
    58  
    59  func testAtomicFloat64() {
    60      f := xatomic.NewAtomicFloat64(0.01)
    61      println(f.Load()) // 0.01
    62      f.Store(2.001)
    63      println(f.Add(0.01)) // 2.011
    64      println(f.Sub(0.01)) // 2.001
    65      println(f.CAS(2.001, 3))
    66  }
    67  
    68  func main() {
    69      testAtomicFloat32()
    70      testAtomicFloat64()
    71  }
    72  ```
    73  
    74  ## Atomic(Int/Uint)(32/64)
    75  
    76  定义
    77  
    78  ``` go
    79  package xatomic
    80  type AtomicInt32 struct{ v int32 }
    81  type AtomicInt64 struct{ v int64 }
    82  type AtomicUint32 struct{ v uint32 }
    83  type AtomicUint64 struct{ v uint64 }
    84  ```
    85  
    86  ## AtomicString
    87  
    88  定义
    89  
    90  ``` go
    91  package xatomic
    92  
    93  import (
    94      "sync/atomic"
    95  )
    96  
    97  type AtomicString struct {
    98  	v atomic.Value
    99  }
   100  ```