You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This is a simple producer/consumer system.package main
import (
"fmt""math/rand""time"
. "github.com/pspaces/gospace"
)
// PRODUCERS defines the number of producers.constPRODUCERS=2// NTASKS defines the number of tasks each producer generates.constNTASKS=2// WORKERS defines the number of workers.constWORKERS=2funcmain() {
rand.Seed(time.Now().UTC().UnixNano())
bag:=NewSpace("bag")
fori:=0; i<PRODUCERS; i++ {
goproducer(&bag, i, NTASKS)
}
fori:=0; i<WORKERS; i++ {
goworker(&bag, i)
}
bag.Query("done")
}
funcproducer(bag*Space, meint, ntasksint) {
fori:=0; i<ntasks; i++ {
x:=rand.Intn(10)
bag.Put(x)
fmt.Printf("Producer %d added %d to the bag...\n", me, x)
}
}
funcworker(bag*Space, meint) {
varxintvaryintfor {
// Get one number.bag.Get(&x)
// Try to get another number._, err:=bag.GetP(&y)
iferr==nil {
// If found then reduce.fmt.Printf("Worker %d got pair (%d,%d) to reduce.\n", me, x, y)
x=x+ybag.Put(x)
fmt.Printf("Worker %d added %d to the bag.\n", me, x)
} else {
// If not found, replace the first number.bag.Put(x)
}
}
}
The text was updated successfully, but these errors were encountered:
The program below crashes:
The text was updated successfully, but these errors were encountered: