Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Metered-concurrency/Go/metered-concurrency-1.go
Normal file
45
Task/Metered-concurrency/Go/metered-concurrency-1.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// counting semaphore implemented with a buffered channel
|
||||
type sem chan struct{}
|
||||
|
||||
func (s sem) acquire() { s <- struct{}{} }
|
||||
func (s sem) release() { <-s }
|
||||
func (s sem) count() int { return cap(s) - len(s) }
|
||||
|
||||
// log package serializes output
|
||||
var fmt = log.New(os.Stdout, "", 0)
|
||||
|
||||
// library analogy per WP article
|
||||
const nRooms = 10
|
||||
const nStudents = 20
|
||||
|
||||
func main() {
|
||||
rooms := make(sem, nRooms)
|
||||
// WaitGroup used to wait for all students to have studied
|
||||
// before terminating program
|
||||
var studied sync.WaitGroup
|
||||
studied.Add(nStudents)
|
||||
// nStudents run concurrently
|
||||
for i := 0; i < nStudents; i++ {
|
||||
go student(rooms, &studied)
|
||||
}
|
||||
studied.Wait()
|
||||
}
|
||||
|
||||
func student(rooms sem, studied *sync.WaitGroup) {
|
||||
rooms.acquire()
|
||||
// report per task descrption. also exercise count operation
|
||||
fmt.Printf("Room entered. Count is %d. Studying...\n",
|
||||
rooms.count())
|
||||
time.Sleep(2 * time.Second) // sleep per task description
|
||||
rooms.release()
|
||||
studied.Done() // signal that student is done
|
||||
}
|
||||
61
Task/Metered-concurrency/Go/metered-concurrency-2.go
Normal file
61
Task/Metered-concurrency/Go/metered-concurrency-2.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var fmt = log.New(os.Stdout, "", 0)
|
||||
|
||||
type countSem struct {
|
||||
int
|
||||
sync.Cond
|
||||
}
|
||||
|
||||
func newCount(n int) *countSem {
|
||||
return &countSem{n, sync.Cond{L: &sync.Mutex{}}}
|
||||
}
|
||||
|
||||
func (cs *countSem) count() int {
|
||||
cs.L.Lock()
|
||||
c := cs.int
|
||||
cs.L.Unlock()
|
||||
return c
|
||||
}
|
||||
|
||||
func (cs *countSem) acquire() {
|
||||
cs.L.Lock()
|
||||
cs.int--
|
||||
for cs.int < 0 {
|
||||
cs.Wait()
|
||||
}
|
||||
cs.L.Unlock()
|
||||
}
|
||||
|
||||
func (cs *countSem) release() {
|
||||
cs.L.Lock()
|
||||
cs.int++
|
||||
cs.L.Unlock()
|
||||
cs.Broadcast()
|
||||
}
|
||||
|
||||
func main() {
|
||||
librarian := newCount(10)
|
||||
nStudents := 20
|
||||
var studied sync.WaitGroup
|
||||
studied.Add(nStudents)
|
||||
for i := 0; i < nStudents; i++ {
|
||||
go student(librarian, &studied)
|
||||
}
|
||||
studied.Wait()
|
||||
}
|
||||
|
||||
func student(studyRoom *countSem, studied *sync.WaitGroup) {
|
||||
studyRoom.acquire()
|
||||
fmt.Printf("Room entered. Count is %d. Studying...\n", studyRoom.count())
|
||||
time.Sleep(2 * time.Second)
|
||||
studyRoom.release()
|
||||
studied.Done()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue