最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

concurrency - Go atomic function pointer - Stack Overflow

programmeradmin6浏览0评论

With atomic.Pointer one can make an atomic pointer to a known type using generics. A func() is essentially already a pointer but is not part of the * syntax so it can't go into the atomic.Pointer. Closest usable data structure seems to be atomic.Value but it's very unergonomic. Is there a readily available tool that I'm missing?

With atomic.Pointer one can make an atomic pointer to a known type using generics. A func() is essentially already a pointer but is not part of the * syntax so it can't go into the atomic.Pointer. Closest usable data structure seems to be atomic.Value but it's very unergonomic. Is there a readily available tool that I'm missing?

Share Improve this question asked Mar 21 at 12:55 Ayberk ÖzgürAyberk Özgür 5,3044 gold badges40 silver badges64 bronze badges 2
  • I realize now that slices and maps have the exact same ergonomics problem. – Ayberk Özgür Commented Mar 21 at 13:48
  • 1 Although function values are implemented as a pointer, there's nothing in the language specification that requires this implementation. Slices contain a pointer, but are not pointers themselves. There is not an ergonomic problem to solve with these types. – Thundercat Commented Mar 21 at 14:56
Add a comment  | 

1 Answer 1

Reset to default 3

func() can't go into atomic.Pointer, but *func() can.

f := func() {
    fmt.Println("hello world")
}
p := atomic.Pointer[func()]{}
p.Store(&f)
g := p.Load()
(*g)()
发布评论

评论列表(0)

  1. 暂无评论