Answer the question
In order to leave comments, you need to log in
Is this code thread safe?
I know that I have already asked a bunch of times about this, but one figs I can not smoke thread safety.
The code:
import (
"html/template"
"sync"
)
var rw = &sync.RWMutex{}
var st = map[string]*template.Template{}
func Get(patch string) (*template.Template, bool) {
rw.RLock()
defer rw.RUnlock()
q, w := st[patch]
return q, w
}
func set(patch string, value *template.Template) {
rw.Lock()
defer rw.Unlock()
st[patch] = value
}
Answer the question
In order to leave comments, you need to log in
When you're working with map, even if you remove the pointer, nothing bad will happen.
But if you change the data under the pointer, then this is not safe in different threads. This requires one more mutex (or many per pointer), or in the Get method, make a copy and pass the copy pointer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question