# Packages
No description provided by the author
# README
MutexMap - A Thread-Safe Map for Go
A thread-safe map implementation for Go, using sync.RWMutex
to synchronize access. This package is optimized for scenarios involving concurrent reads and writes, providing efficient and reliable operations for multi-thread applications.
README
Overview
Go’s standard map
is not safe for concurrent access. This package wraps a map[K]V
with sync.RWMutex
to provide thread safety. With RWMutex
, multiple readers can access the map simultaneously, while writes are synchronized to prevent race conditions.
Key highlights:
- Thread-safety: Prevents data races in concurrent environments.
- Efficient Reads: Read operations (
Get
,Range
) are lock-free for other readers. - Synchronous Write: Write operations (
Set
,Delete
,Getset
) are synchronous.
This package is ideal for use cases requiring frequent reads and occasional writes with a shared map.
Installation
go get github.com/yyle88/mutexmap
Example Usage
Basic Operations
package main
import (
"fmt"
"github.com/yyle88/mutexmap"
)
func main() {
mp := mutexmap.NewMap
mp.Set("key1", 100)
mp.Set("key2", 200)
if value, found := mp.Get("key1"); found {
fmt.Println("Key1 Value:", value)
}
mp.Range(func(key string, value int) bool {
fmt.Println(key, value)
return true
})
}
Using Getset
for Cached Initialization
package main
import (
"fmt"
"github.com/yyle88/mutexmap"
)
func main() {
mp := mutexmap.NewMap
value, created := mp.Getset("exampleKey", func() string {
return "This is a computed value"
})
fmt.Println("Created:", created, "Value:", value)
value, created = mp.Getset("exampleKey", func() string {
return "Another computed value"
})
fmt.Println("Created:", created, "Value:", value)
}
Features
- Concurrent Access: Allows multiple goroutines to safely read and write to the map.
- Optimized Reads: Supports simultaneous reads for better performance.
- Custom Initialization: Use
Getset
to initialize values only if they don’t already exist.
Method Summary
Method | Description |
---|---|
NewMap[K comparable, V any](cap int) | Creates a new Map with an optional initial capacity. |
Get(k K) (V, bool) | Retrieves the value for the given key. Returns false if the key is not found. |
Set(k K, v V) | Sets a value for the given key. Overwrites if the key already exists. |
Delete(k K) | Removes the key-value pair from the map. |
Len() int | Returns the number of elements in the map. |
Range(func(k K, v V) bool) | Iterates over all key-value pairs. Stops if the callback returns false . |
Getset(k K, func() V) (V, bool) | Gets a value or creates it if it doesn’t exist, ensuring the creation is atomic and thread-safe. |
Why Use MutexMap?
- Thread Safety: Essential for shared maps in multi-threaded environments.
- Efficient Reads: Read lock (
RLock
) ensures non-blocking reads for other readers. - Write Synchronization: Write lock (
Lock
) ensures data integrity during modifications. - Flexible Initialization: The
Getset
method prevents redundant computations.
Contributing
Welcome to submit issues or requests for improvements!
If you find this package valuable, give it a ⭐ on GitHub! Thank you for your support!!!