Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Value ¶
type Value[T any] struct { // contains filtered or unexported fields }
Value 是对 atomic.Value 的泛型封装 相比直接使用 atomic.Value, - Load 方法大概开销多了 0.5 ns - Store 方法多了不到 2 ns - Swap 方法多了 14 ns - CompareAndSwap 在失败的情况下,会多 2 ns,成功的时候多了 0.3 ns 使用 NewValue 或者 NewValueOf 来创建实例
func NewValue ¶
NewValue 会创建一个 Value 对象,里面存放着 T 的零值 注意,这个零值是带了类型的零值
Example ¶
val := NewValue[int]() data := val.Load() fmt.Println(data)
Output: 0
func NewValueOf ¶
NewValueOf 会使用传入的值来创建一个 Value 对象
Example ¶
val := NewValueOf[int](123) data := val.Load() fmt.Println(data)
Output: 123
func (*Value[T]) CompareAndSwap ¶
Example ¶
val := NewValueOf[int](123) swapped := val.CompareAndSwap(123, 456) fmt.Println(swapped) swapped = val.CompareAndSwap(455, 459) fmt.Println(swapped)
Output: true false
func (*Value[T]) Load ¶
func (v *Value[T]) Load() (val T)
Example ¶
val := NewValueOf[int](123) data := val.Load() fmt.Println(data)
Output: 123
Click to show internal directories.
Click to hide internal directories.