Concurrency Coordination Reports
RWMutex Read Coordination Report
Record shared read guards against a write probe and report the read status.
RWMutex Read Coordination Report
rwmutex_read_coordination_report.go
package main
import (
"fmt"
"sync"
)
func main() {
var readers =
var lock sync.RWMutex
active := 0
for i := 0; i < readers; i++ {
ok := lock.TryRLock()
if ok {
active++
}
}
wok := lock.TryLock()
writeBlocked := !wok
var status string
if active == 0 {
status = "writable"
} else if active == 1 {
status = "single"
} else {
status = "shared"
}
fmt.Printf("readers=%d write_blocked=%t %s\n", active, writeBlocked, status)
}
package main
import (
"fmt"
"sync"
)
func main() {
var readers =
var lock sync.RWMutex
active := 0
for i := 0; i < readers; i++ {
ok := lock.TryRLock()
if ok {
active++
}
}
wok := lock.TryLock()
writeBlocked := !wok
var status string
if active == 0 {
status = "writable"
} else if active == 1 {
status = "single"
} else {
status = "shared"
}
fmt.Printf("readers=%d write_blocked=%t %s\n", active, writeBlocked, status)
}
package main
import (
"fmt"
"sync"
)
func main() {
var readers =
var lock sync.RWMutex
active := 0
for i := 0; i < readers; i++ {
ok := lock.TryRLock()
if ok {
active++
}
}
wok := lock.TryLock()
writeBlocked := !wok
var status string
if active == 0 {
status = "writable"
} else if active == 1 {
status = "single"
} else {
status = "shared"
}
fmt.Printf("readers=%d write_blocked=%t %s\n", active, writeBlocked, status)
}
shared reads
`sync.RWMutex.TryRLock` grants multiple read locks at once, so several readers can hold the lock together. A `TryLock` write probe is rejected while any read lock is held, so the report shows the write as blocked. The number of live read locks drives the coordination status from writable through single to shared.