TryLock is a non-blocking mutex operation. It acquires an available mutex and returns true; when another goroutine holds the mutex, it returns false immediately instead of blocking. section 4.1.3
Use TryLock deliberately:
- Let a monitor goroutine inspect shared progress without adding contention; retry later when the mutex is busy. section 4.1.3
- Unlock the mutex after a successful TryLock, just as with Lock; otherwise the protected critical section remains locked. section 4.1.3
- Treat frequent use as a warning sign: ordinary goroutines are cheap to create, so another goroutine may be preferable to polling. section 4.1.3
Monitoring a frequency table
Several goroutines count letters from downloaded RFC pages while a monitor periodically calls mutex.TryLock(). On success it prints the frequency table and unlocks; on failure it reports that the mutex is busy and tries again later. The output therefore mixes completed downloads, updated counts, and occasional “Mutex already being used” messages.
