routinegoroutineGolanggoidC++/JavaThreadLocalGolangThreadLocalContextroutinegoroutine local storageGolangroutineroutine.Goid()routine.AllGoids()mainGoid()1AllGoids()18LocalStorageLocalStorage执行结果为:
routineGoid() (id int64)goroutinegoidGoid()go_tlsrand.Int()Goid()runtime.StackAllGoids() (ids []int64)goroutinegoidgo 1.15AllGoids()runtime.Stackgo 1.16AllGoids()nativeruntimeNewLocalStorage()LocalStorageThreadLocalBackupContext() *ImmutableContextlocal storageInheritContext(ic *ImmutableContext)local storageBackupContext()Go(f func())local storageBackupContext()InheritContext()LocalStorage表示协程上下文变量,支持的函数包括:
Get() (value interface{})nilSet(v interface{}) interface{}Del() (v interface{})Clear()Get/Set/Delroutinestoragesgoidptr在进程的整个生命周期中,它可能会创建于销毁无数个协程,那么这些协程的上下文变量如何清理呢?
routineGCTimerstoragesdeadstorages以上就是关于Golang 中的”ThreadLocal“库的详细内容,现在想要学习的程序员欢迎关注六星社区(https://six.club/),获取更多技能与教程。