Programming/golang

golang mutex

구차니 2026. 8. 25. 11:11

sync  패키지

[링크 : https://pkg.go.dev/sync]

 

Mutex.Lock() 은 Mutex.Unlock()으로 풀수 있다.

[링크 : https://pkg.go.dev/sync#Mutex.Lock]

[링크 : https://pkg.go.dev/sync#Mutex]

 

RWMutex.RLock() / RWMutex.Lock() - RWMutex.RUnlock() / RWMutex.Unlock()

abc 순으로 정렬되어있다보니 보기가 어렵네

아무튼 Lock - Unlock / RLock - RUnlock 으로 pair가 된다.

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.


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.

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.


func (*RWMutex) RLock ¶
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.


func (*RWMutex) RUnlock ¶
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.

[링크 : https://pkg.go.dev/sync#RWMutex.Lock]

[링크 : https://pkg.go.dev/sync#RWMutex]

 

[링크 : https://www.jaenung.net/tree/28536]

[링크 : https://rainbow96bear.tistory.com/entry/Go-고루틴-이해하기-뮤텍스-데드락]

 

+

2026.08.26

Usage¶
To help diagnose such bugs, Go includes a built-in data race detector. To use it, add the -race flag to the go command:

$ go test -race mypkg    // to test the package
$ go run -race mysrc.go  // to run the source file
$ go build -race mycmd   // to build the command
$ go install -race mypkg // to install the package

[링크 : https://go.dev/doc/articles/race_detector]