Ai
1 Star 3 Fork 0

TimAndy/routine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_thread_local.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
package routine
// ThreadLocal provides goroutine-local variables.
type ThreadLocal[T any] interface {
// Get returns the value in the current goroutine's local threadLocals or inheritableThreadLocals, if it was set before.
Get() T
// Set copy the value into the current goroutine's local threadLocals or inheritableThreadLocals.
Set(value T)
// Remove delete the value from the current goroutine's local threadLocals or inheritableThreadLocals.
Remove()
}
// Supplier provides a function that returns a value of type T.
type Supplier[T any] func() T
// NewThreadLocal create and return a new ThreadLocal instance.
// The initial value stored with the default value of type T.
func NewThreadLocal[T any]() ThreadLocal[T] {
return &threadLocal[T]{index: nextThreadLocalIndex()}
}
// NewThreadLocalWithInitial create and return a new ThreadLocal instance.
// The initial value stored as the return value of the method supplier.
func NewThreadLocalWithInitial[T any](supplier Supplier[T]) ThreadLocal[T] {
return &threadLocal[T]{index: nextThreadLocalIndex(), supplier: supplier}
}
// NewInheritableThreadLocal create and return a new ThreadLocal instance.
// The initial value stored with the default value of type T.
// The value can be inherited to sub goroutines witch started by Go, GoWait, GoWaitResult methods.
// The value can be captured to FutureTask which created by WrapTask, WrapWaitTask, WrapWaitResultTask methods.
func NewInheritableThreadLocal[T any]() ThreadLocal[T] {
return &inheritableThreadLocal[T]{index: nextInheritableThreadLocalIndex()}
}
// NewInheritableThreadLocalWithInitial create and return a new ThreadLocal instance.
// The initial value stored as the return value of the method supplier.
// The value can be inherited to sub goroutines witch started by Go, GoWait, GoWaitResult methods.
// The value can be captured to FutureTask which created by WrapTask, WrapWaitTask, WrapWaitResultTask methods.
func NewInheritableThreadLocalWithInitial[T any](supplier Supplier[T]) ThreadLocal[T] {
return &inheritableThreadLocal[T]{index: nextInheritableThreadLocalIndex(), supplier: supplier}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/timandy/routine.git
git@gitee.com:timandy/routine.git
timandy
routine
routine
main

搜索帮助