Concurrency Coordination Reports
Mutex Try-Lock Coordination Report
Record nonblocking lock attempts and report whether a later attempt was blocked.
Mutex Try-Lock Coordination Report
mutex_trylock_coordination_report.go
package main
import (
"fmt"
"sync"
)
func main() {
var holds =
var lock sync.Mutex
acquired := 0
blocked := 0
for i := 0; i < holds; i++ {
ok := lock.TryLock()
if ok {
acquired++
} else {
blocked++
}
}
var status string
if acquired == 0 {
status = "idle"
} else if blocked > 0 {
status = "blocked"
} else {
status = "guarded"
}
fmt.Printf("holds=%d acquired=%d blocked=%d %s\n", holds, acquired, blocked, status)
}
package main
import (
"fmt"
"sync"
)
func main() {
var holds =
var lock sync.Mutex
acquired := 0
blocked := 0
for i := 0; i < holds; i++ {
ok := lock.TryLock()
if ok {
acquired++
} else {
blocked++
}
}
var status string
if acquired == 0 {
status = "idle"
} else if blocked > 0 {
status = "blocked"
} else {
status = "guarded"
}
fmt.Printf("holds=%d acquired=%d blocked=%d %s\n", holds, acquired, blocked, status)
}
package main
import (
"fmt"
"sync"
)
func main() {
var holds =
var lock sync.Mutex
acquired := 0
blocked := 0
for i := 0; i < holds; i++ {
ok := lock.TryLock()
if ok {
acquired++
} else {
blocked++
}
}
var status string
if acquired == 0 {
status = "idle"
} else if blocked > 0 {
status = "blocked"
} else {
status = "guarded"
}
fmt.Printf("holds=%d acquired=%d blocked=%d %s\n", holds, acquired, blocked, status)
}
nonblocking acquire
A coordination report records nonblocking lock attempts. `sync.Mutex.TryLock` returns true when it takes the lock and false when the lock is already held, so it never blocks. Each acquired lock stays held, so a later attempt in the same run is reported as blocked and the status line summarizes whether the section stayed idle, guarded, or blocked a later acquire.