geektutu / blog

极客兔兔的博客,Coding Coding 创建有趣的开源项目。
https://geektutu.com
Apache License 2.0
166 stars 21 forks source link

Go sync.Once | Go 语言高性能编程 | 极客兔兔 #122

Open geektutu opened 3 years ago

geektutu commented 3 years ago

https://geektutu.com/post/hpg-sync-once.html

Go 语言/golang 高性能编程,Go 语言进阶教程,Go 语言高性能编程(high performance go)。sync.Once 是 Golang package 中使方法只执行一次的对象实现,作用与 init 函数类似,但也有所不同。本文还解释了 sync.Once 源码中,done 为什么作为第一个字段。

RightAirBlack commented 3 years ago

又一个结构体字段放置小技巧get![doge]

wgliyuli commented 2 years ago

又一个结构体字段放置小技巧get![doge]

lanlanlanlanlanchengzhu commented 2 years ago

什么叫做hot path呀?

honkkki commented 2 years ago

为什么done要用uint32类型呢 如果简单是标志位0 1为什么不是uint8

jlvihv commented 2 years ago

@honkkki 为什么done要用uint32类型呢 如果简单是标志位0 1为什么不是uint8

也许是为了内存对齐??

ethushiroha commented 1 year ago

@honkkki 为什么done要用uint32类型呢 如果简单是标志位0 1为什么不是uint8

因为用到了 atomic 包吧, atomic 包最小的类型就是 uint32 了