Documentation
¶
Overview ¶
Package threadsafe defines lock interfaces for building reusable, goroutine-safe data structures and helpers without hard-coding a concrete lock type.
It defines two lock interfaces:
- Locker: the write-lock contract (`Lock` and `Unlock`), a defined type sharing the method set of sync.Locker.
- RLocker: the read-lock contract (`RLock` and `RUnlock`) used by read/write synchronization patterns.
These interfaces are embedded or referenced by concurrent containers and utility types used across multiple goroutines.
Usage ¶
See the examples in:
- github.com/tecnickcom/nurago/pkg/threadsafe/tsmap
- github.com/tecnickcom/nurago/pkg/threadsafe/tsslice
Read helpers in dependent packages accept RLocker, which a plain sync.Mutex does not satisfy (it has no `RLock`/`RUnlock`). This forces callers that need shared read access to supply an sync.RWMutex-like type at compile time, rather than serializing every read behind an exclusive lock. A sync.RWMutex satisfies both Locker and RLocker, so the same lock instance can drive read and write helpers.
These interfaces only describe the locking contract; they do not enforce it. All access to the protected data must funnel through the helper functions using the same lock instance, otherwise concurrent reads and writes are not safe.
When To Use ¶
- You are writing a goroutine-safe type and want the caller to choose sync.Mutex or sync.RWMutex.
- Read-heavy code should be able to declare that it only needs a shared lock.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Locker ¶
Locker defines exclusive lock semantics with Lock and Unlock methods.
Example ¶
package main
import (
"fmt"
"sync"
"github.com/tecnickcom/nurago/pkg/threadsafe"
)
// counter accepts any lock satisfying threadsafe.Locker, so callers choose
// between sync.Mutex, sync.RWMutex, or a custom lock without the type being
// hard-coded here.
type counter struct {
mux threadsafe.Locker
values map[string]int
}
func (c *counter) inc(key string) {
c.mux.Lock()
defer c.mux.Unlock()
c.values[key]++
}
func main() {
c := &counter{
mux: &sync.Mutex{},
values: make(map[string]int),
}
var wg sync.WaitGroup
for range 100 {
wg.Go(func() {
c.inc("hits")
})
}
wg.Wait()
fmt.Println(c.values["hits"])
}
Output: 100
type RLocker ¶
type RLocker interface {
RLock()
RUnlock()
}
RLocker defines shared read-lock semantics with RLock and RUnlock methods.
Example ¶
package main
import (
"fmt"
"sync"
"github.com/tecnickcom/nurago/pkg/threadsafe"
"github.com/tecnickcom/nurago/pkg/threadsafe/tsmap"
)
// stats reads under a shared lock, so it declares threadsafe.RLocker and
// concurrent readers do not block each other.
type stats struct {
mux threadsafe.RLocker
values map[string]int
}
func (s *stats) get(key string) int {
s.mux.RLock()
defer s.mux.RUnlock()
return s.values[key]
}
func main() {
mux := &sync.RWMutex{}
s := &stats{
mux: mux,
values: map[string]int{"hits": 7},
}
// The same sync.RWMutex satisfies Locker for writes through tsmap.
tsmap.Set(mux, s.values, "misses", 3)
fmt.Println(s.get("hits"), s.get("misses"))
}
Output: 7 3
Directories
¶
| Path | Synopsis |
|---|---|
|
Package tsmap reads and writes maps shared across goroutines, taking a caller-supplied lock at every call site.
|
Package tsmap reads and writes maps shared across goroutines, taking a caller-supplied lock at every call site. |
|
Package tsslice reads and writes slices shared across goroutines, taking a caller-supplied lock at every access.
|
Package tsslice reads and writes slices shared across goroutines, taking a caller-supplied lock at every access. |