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

Function start

chapter7/patterns/semaphore/semaphore.go:87–108  ·  view source on GitHub ↗

start uses the generator pattern to create the readerWriter value. It launches goroutines to process the work, returning the created ReaderWriter value.

(name string, maxReads int, maxReaders int)

Source from the content-addressed store, hash-verified

85// start uses the generator pattern to create the readerWriter value. It launches
86// goroutines to process the work, returning the created ReaderWriter value.
87func start(name string, maxReads int, maxReaders int) *readerWriter {
88 // Create a value of readerWriter and initialize.
89 rw := readerWriter{
90 name: name,
91 shutdown: make(chan struct{}),
92 maxReads: maxReads,
93 maxReaders: maxReaders,
94 readerControl: make(semaphore, maxReads),
95 }
96
97 // Launch a number of reader goroutines and let them start reading.
98 rw.reportShutdown.Add(maxReaders)
99 for goroutine := 0; goroutine < maxReaders; goroutine++ {
100 go rw.reader(goroutine)
101 }
102
103 // Launch the single writer goroutine and let it start writing.
104 rw.reportShutdown.Add(1)
105 go rw.writer()
106
107 return &rw
108}
109
110// shutdown stops all of the existing readerWriter processes concurrently.
111func shutdown(readerWriters ...*readerWriter) {

Callers 1

mainFunction · 0.85

Calls 3

readerMethod · 0.95
writerMethod · 0.95
AddMethod · 0.80

Tested by

no test coverage detected