Skip to content

Repository files navigation

Go ThreadLocal

Go version of ThreadLocal is like Java ThreadLocal

Usage

import (
"fmt""sync""github.com/cyub/threadlocal"
)
varwg sync.WaitGroupfuncmain() {
tl1:=threadlocal.New()
tl1.Set("hello, world")
n:=10wg.Add(n)
fori:=0; i<n; i++ {
gofunc(idxint) {
deferwg.Done()
fmt.Printf("id: %d, tid: %d, val: %v\n", idx, threadlocal.ThreadId(), tl1.Get())
}(i)
}
wg.Wait()
fmt.Printf("id: %d, tid: %d, val: %v\n", n, threadlocal.ThreadId(), tl1.Get())
}

You can run the following line of code to test:

go run example/main.go

About

Go version of ThreadLocal is like Java ThreadLocal

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages