Metered concurrency: Difference between revisions

Content deleted Content added
Sonia (talk | contribs)
Go solution
Sonia (talk | contribs)
→‎{{header|Go}}: updated to use new WaitGroup feature
Line 335:
"fmt"
"time"
"sync"
)
 
Line 346 ⟶ 347:
go librarian(acquire, release, count, 10)
nStudents := 20
var studied sync.WaitGroup
 
studied := make.Add(chan intnStudents) // done channel
for i := 0; i < nStudents; i++ {
go student(acquire, release, studied)
}
 
// wait until all students are done
for i := 0; i < nStudents; i++ {
<-go student(acquire, release, &studied)
}
// wait until all students arehave donestudied before terminating program
studied.Wait()
}
 
Line 367 ⟶ 365:
p = nil
}
case <-r: // release/v operation
count++
p = a
Line 376 ⟶ 374:
}
 
func student(a, r, s chan int, studied *sync.WaitGroup) {
a <- 0 // accquire
fmt.Println("Studying...") // report per task descrption
time.Sleep(2e9) // sleep per task description
r <- 0 // release
s <- 0 studied.Done() // signal done
}</lang>
Output shows 10 students studying immediately, about a 2 second pause, 10 more students studying, then another pause of about 2 seconds before returning to the command prompt.