sync
import "sync"
Package sync provides basic synchronization primitives such as mutual exclusion locks. Other than the [Once] and [WaitGroup] types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication. Values containing the types defined in this package should not be copied.
Go 1.26.1
Functions
func NewCond(l Locker) *Cond
NewCond returns a new Cond with Locker l.
sync/cond.go:48
func OnceFunc(f func(...)) func(...)
OnceFunc returns a function that invokes f only once. The returned function may be called concurrently. If f panics, the returned function will panic with the same value on every call.
sync/oncefunc.go:11
func OnceValue(f func(...)) func(...)
OnceValue returns a function that invokes f only once and returns the value returned by f. The returned function may be called concurrently. If f panics, the returned function will panic with the same value on every call.
sync/oncefunc.go:46
func OnceValues(f func(...)) func(...)
OnceValues returns a function that invokes f only once and returns the values returned by f. The returned function may be called concurrently. If f panics, the returned function will panic with the same value on every call.
sync/oncefunc.go:80
Types
type Cond struct {
// L is held while observing or changing the condition
L Locker
// contains filtered or unexported fields
}Cond implements a condition variable, a rendezvous point for goroutines waiting for or announcing the occurrence of an event. Each Cond has an associated Locker L (often a [*Mutex] or [*RWMutex]), which must be held when changing the condition and when calling the [Cond.Wait] method. A Cond must not be copied after first use. In the terminology of [the Go memory model], Cond arranges that a call to [Cond.Broadcast] or [Cond.Signal] “synchronizes before” any Wait call that it unblocks. ...
sync/cond.go:37
func (c *Cond) Broadcast()
Broadcast wakes all goroutines waiting on c. It is allowed but not required for the caller to hold c.L during the call.
sync/cond.go:91
func (c *Cond) Signal()
Signal wakes one goroutine waiting on c, if there is any. It is allowed but not required for the caller to hold c.L during the call. Signal() does not affect goroutine scheduling priority; if other goroutines are attempting to lock c.L, they may be awoken before a "waiting" goroutine.
sync/cond.go:82
func (c *Cond) Wait()
Wait atomically unlocks c.L and suspends execution of the calling goroutine. After later resuming execution, Wait locks c.L before returning. Unlike in other systems, Wait cannot return unless awoken by [Cond.Broadcast] or [Cond.Signal]. Because c.L is not locked while Wait is waiting, the caller typically cannot assume that the condition is true when Wait returns. Instead, the caller should Wait in a loop: c.L.Lock() for !condition() { c.Wait() } ... make use of condition ... c.L...
sync/cond.go:67
type Locker interface {
Lock()
Unlock()
}A Locker represents an object that can be locked and unlocked.
sync/mutex.go:37
type Map struct {
// contains filtered or unexported fields
}Map is like a Go map[any]any but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time. The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content. The Map type is optimized for two common use cases: (1) when the entry for a given key is only...
sync/map.go:38
func (m *Map) Clear()
Clear deletes all the entries, resulting in an empty Map.
sync/map.go:57
func (m *Map) CompareAndDelete(key any, old any) bool
CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type. If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).
sync/map.go:98
func (m *Map) CompareAndSwap(key any, old any, new any) bool
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
sync/map.go:89
func (m *Map) Delete(key any)
Delete deletes the value for a key. If the key is not in the map, Delete does nothing.
sync/map.go:76
func (m *Map) Load(key any) (any, bool)
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
sync/map.go:47
func (m *Map) LoadAndDelete(key any) (any, bool)
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
sync/map.go:70
func (m *Map) LoadOrStore(key any, value any) (any, bool)
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
sync/map.go:64
func (m *Map) Range(f func(...))
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m...
sync/map.go:113
func (m *Map) Store(key any, value any)
Store sets the value for a key.
sync/map.go:52
func (m *Map) Swap(key any, value any) (any, bool)
Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.
sync/map.go:82
type Mutex struct {
// contains filtered or unexported fields
}A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex. A Mutex must not be copied after first use. In the terminology of [the Go memory model], the n'th call to [Mutex.Unlock] “synchronizes before” the m'th call to [Mutex.Lock] for any n < m. A successful call to [Mutex.TryLock] is equivalent to a call to Lock. A failed call to TryLock does not establish any “synchronizes before” relation at all. [the Go memory model]: https://go.dev/ref/mem
sync/mutex.go:30
func (m *Mutex) Lock()
Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.
sync/mutex.go:45
func (m *Mutex) TryLock() bool
TryLock tries to lock m and reports whether it succeeded. Note that while correct uses of TryLock do exist, they are rare, and use of TryLock is often a sign of a deeper problem in a particular use of mutexes.
sync/mutex.go:54
func (m *Mutex) Unlock()
Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock. A locked [Mutex] is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.
sync/mutex.go:64
type Once struct {
// contains filtered or unexported fields
}Once is an object that will perform exactly one action. A Once must not be copied after first use. In the terminology of [the Go memory model], the return from f “synchronizes before” the return from any call of once.Do(f). [the Go memory model]: https://go.dev/ref/mem
sync/once.go:20
func (o *Once) Do(f func(...))
Do calls the function f if and only if Do is being called for the first time for this instance of [Once]. In other words, given var once Once if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new instance of Once is required for each function to execute. Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to ...
sync/once.go:52
type Pool struct {
// New optionally specifies a function to generate
// a value when Get would otherwise return nil.
// It may not be changed concurrently with calls to Get.
New func() any
/...A Pool is a set of temporary objects that may be individually saved and retrieved. Any item stored in the Pool may be removed automatically at any time without notification. If the Pool holds the only reference when this happens, the item might be deallocated. A Pool is safe for use by multiple goroutines simultaneously. Pool's purpose is to cache allocated but unused items for later reuse, relieving pressure on the garbage collector. That is, it makes it easy to build efficient, thread-sa...
sync/pool.go:51
func (p *Pool) Get() any
Get selects an arbitrary item from the [Pool], removes it from the Pool, and returns it to the caller. Get may choose to ignore the pool and treat it as empty. Callers should not assume any relation between values passed to [Pool.Put] and the values returned by Get. If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.
sync/pool.go:131
func (p *Pool) Put(x any)
Put adds x to the pool.
sync/pool.go:99
type RWMutex struct {
// contains filtered or unexported fields
}A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex. A RWMutex must not be copied after first use. If any goroutine calls [RWMutex.Lock] while the lock is already held by one or more readers, concurrent calls to [RWMutex.RLock] will block until the writer has acquired (and released) the lock, to ensure that the lock eventually becomes available to the writer. Note tha...
sync/rwmutex.go:39
func (rw *RWMutex) Lock()
Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.
sync/rwmutex.go:144
func (rw *RWMutex) RLock()
RLock locks rw for reading. It should not be used for recursive read locking; a blocked Lock call excludes new readers from acquiring the lock. See the documentation on the [RWMutex] type.
sync/rwmutex.go:67
func (rw *RWMutex) RLocker() Locker
RLocker returns a [Locker] interface that implements the [Locker.Lock] and [Locker.Unlock] methods by calling rw.RLock and rw.RUnlock.
sync/rwmutex.go:240
func (rw *RWMutex) RUnlock()
RUnlock undoes a single [RWMutex.RLock] call; it does not affect other simultaneous readers. It is a run-time error if rw is not locked for reading on entry to RUnlock.
sync/rwmutex.go:114
func (rw *RWMutex) TryLock() bool
TryLock tries to lock rw for writing and reports whether it succeeded. Note that while correct uses of TryLock do exist, they are rare, and use of TryLock is often a sign of a deeper problem in a particular use of mutexes.
sync/rwmutex.go:169
func (rw *RWMutex) TryRLock() bool
TryRLock tries to lock rw for reading and reports whether it succeeded. Note that while correct uses of TryRLock do exist, they are rare, and use of TryRLock is often a sign of a deeper problem in a particular use of mutexes.
sync/rwmutex.go:87
func (rw *RWMutex) Unlock()
Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing on entry to Unlock. As with Mutexes, a locked [RWMutex] is not associated with a particular goroutine. One goroutine may [RWMutex.RLock] ([RWMutex.Lock]) a RWMutex and then arrange for another goroutine to [RWMutex.RUnlock] ([RWMutex.Unlock]) it.
sync/rwmutex.go:201
type WaitGroup struct {
// contains filtered or unexported fields
}A WaitGroup is a counting semaphore typically used to wait for a group of goroutines or tasks to finish. Typically, a main goroutine will start tasks, each in a new goroutine, by calling [WaitGroup.Go] and then wait for all tasks to complete by calling [WaitGroup.Wait]. For example: var wg sync.WaitGroup wg.Go(task1) wg.Go(task2) wg.Wait() A WaitGroup may also be used for tracking tasks without using Go to start new goroutines by using [WaitGroup.Add] and [WaitGroup.Done]. The previou...
sync/waitgroup.go:48
func (wg *WaitGroup) Add(delta int)
Add adds delta, which may be negative, to the [WaitGroup] task counter. If the counter becomes zero, all goroutines blocked on [WaitGroup.Wait] are released. If the counter goes negative, Add panics. Callers should prefer [WaitGroup.Go]. Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means t...
sync/waitgroup.go:77
func (wg *WaitGroup) Done()
Done decrements the [WaitGroup] task counter by one. It is equivalent to Add(-1). Callers should prefer [WaitGroup.Go]. In the terminology of [the Go memory model], a call to Done "synchronizes before" the return of any Wait call that it unblocks. [the Go memory model]: https://go.dev/ref/mem
sync/waitgroup.go:155
func (wg *WaitGroup) Go(f func(...))
Go calls f in a new goroutine and adds that task to the [WaitGroup]. When f returns, the task is removed from the WaitGroup. The function f must not panic. If the WaitGroup is empty, Go must happen before a [WaitGroup.Wait]. Typically, this simply means Go is called to start tasks before Wait is called. If the WaitGroup is not empty, Go may happen at any time. This means a goroutine started by Go may itself call Go. If a WaitGroup is reused to wait for several independent sets of tasks, new...
sync/waitgroup.go:236
func (wg *WaitGroup) Wait()
Wait blocks until the [WaitGroup] task counter is zero.
sync/waitgroup.go:160