This commit is contained in:
oneao committed 2025-08-29 17:33:27 +08:00
1 parent 50fdacac07
commit 48cd564201
13 files changed
+220 -122

No files matched your search

@@ -1,3 +1,7 @@
/**
保证多routine共用同一数据
*/
package routinex
import (
@@ -7,7 +11,7 @@ import (
"sync"
)
// ------------------- goroutine id -------------------
// CurGID 获取 routine 的id
func CurGID() uint64 {
var buf [64]byte
n := runtime.Stack(buf[:], false)
@@ -25,6 +29,7 @@ var (
instanceOnce sync.Once
)
// GetInstance 获取当前示例
func GetInstance() *RoutineLocal {
instanceOnce.Do(func() {
instance = &RoutineLocal{}
@@ -57,7 +62,7 @@ func Clear() {
GetInstance().data.Delete(CurGID())
}
// Go 封装函数,自动继承父 goroutine 数据
// Go 自动继承父 goroutine 数据,多 routine 的时候需要使用
func Go(f func()) {
parentGID := CurGID()
parentData, _ := GetInstance().data.Load(parentGID)