Исходники и презентации
This commit is contained in:
25
lessons/sync_primitives/rlocker/main.go
Normal file
25
lessons/sync_primitives/rlocker/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "sync"
|
||||
|
||||
func withLock(mutex sync.Locker, action func()) {
|
||||
if action == nil {
|
||||
return
|
||||
}
|
||||
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
action()
|
||||
}
|
||||
|
||||
func main() {
|
||||
mutex := sync.RWMutex{}
|
||||
withLock(&mutex, func() {
|
||||
// write lock
|
||||
})
|
||||
|
||||
withLock(mutex.RLocker(), func() {
|
||||
// read lock
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user