Package sync
import "sync"
The sync package provides basic synchronization primitives such as mutual exclusion locks. Other than the Once type, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.
Package files
mutex.go once.go rwmutex.gotype Mutex
A Mutex is a mutual exclusion lock. Mutexes can be created as part of other structures; the zero value for a Mutex is an unlocked mutex.
type Mutex struct {
// contains unexported fields
}
func (*Mutex) Lock
func (m *Mutex) Lock()
Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.
func (*Mutex) Unlock
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.
type Once
Once is an object that will perform exactly one action.
type Once struct {
// contains unexported fields
}
func (*Once) Do
func (o *Once) Do(f func())
Do calls the function f if and only if the method is being called for the first time with this receiver. In other words, given
var once Once
if 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 a function to be invoked by Do:
config.once.Do(func() { config.init(filename) })
Because no call to Do returns until the one call to f returns, if f causes Do to be called, it will deadlock.
type RWMutex
An RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. RWMutexes can be created as part of other structures; the zero value for a RWMutex is an unlocked mutex.
Writers take priority over Readers: no new RLocks are granted while a blocked Lock call is waiting.
type RWMutex struct {
// contains unexported fields
}
func (*RWMutex) Lock
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. To ensure that the lock eventually becomes available, a blocked Lock call excludes new readers from acquiring the lock.
func (*RWMutex) RLock
func (rw *RWMutex) RLock()
RLock locks rw for reading. If the lock is already locked for writing or there is a writer already waiting to r the lock, RLock blocks until the writer has released the lock.
func (*RWMutex) RUnlock
func (rw *RWMutex) RUnlock()
RUnlock undoes a single 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.
func (*RWMutex) Unlock
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.
Like for Mutexes, a locked RWMutex is not associated with a particular goroutine. It is allowed for one goroutine to RLock (Lock) an RWMutex and then arrange for another goroutine to RUnlock (Unlock) it.
