MCPcopy Create free account
hub / github.com/goinaction/code / performRead

Method performRead

chapter7/patterns/semaphore/semaphore.go:163–180  ·  view source on GitHub ↗

performRead performs the actual reading work.

(reader int)

Source from the content-addressed store, hash-verified

161
162// performRead performs the actual reading work.
163func (rw *readerWriter) performRead(reader int) {
164 // Get a read lock for this critical section.
165 rw.ReadLock(reader)
166
167 // Safely increment the current reads counter
168 count := atomic.AddInt32(&rw.currentReads, 1)
169
170 // Simulate some reading work
171 log.Printf("%s\t: [%d] Start\t- [%d] Reads\n", rw.name, reader, count)
172 time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
173
174 // Safely decrement the current reads counter
175 count = atomic.AddInt32(&rw.currentReads, -1)
176 log.Printf("%s\t: [%d] Finish\t- [%d] Reads\n", rw.name, reader, count)
177
178 // Release the read lock for this critical section.
179 rw.ReadUnlock(reader)
180}
181
182// writer is a goroutine that listens on the shutdown channel and
183// performs writes until the channel is signaled.

Callers 1

readerMethod · 0.95

Calls 2

ReadLockMethod · 0.95
ReadUnlockMethod · 0.95

Tested by

no test coverage detected