more attempts at wait
This commit is contained in:
parent
a2bd8f9c29
commit
741286521a
|
@ -4,13 +4,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
var functionChan chan func (*sync.WaitGroup)
|
|
||||||
|
|
||||||
type funcWait struct {
|
type funcWait struct {
|
||||||
f func()
|
f func()
|
||||||
wgF sync.WaitGroup
|
wgF sync.WaitGroup
|
||||||
|
@ -18,41 +15,52 @@ type funcWait struct {
|
||||||
res int
|
res int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var functionChan chan *funcWait
|
||||||
|
var afunc funcWait
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
func generateNumbers(total int, ch chan<- func (), wg *sync.WaitGroup) {
|
func generateNumbers(total int, ch chan<- *funcWait, af *funcWait) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
fmt.Printf("sending %d to channel\n", total)
|
log.Println("generateNumbers() START total =", total)
|
||||||
ch <- func() {
|
ch <- af
|
||||||
fmt.Printf("inside f() sending %d to channel\n", total)
|
log.Println("generateNumbers() END total =", total)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func andlabsGoroutine(ch <-chan func(), wg *sync.WaitGroup) {
|
func andlabsGoroutine(ch <-chan *funcWait, wgF *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
|
log.Println("andlabsGoroutine() START")
|
||||||
for f := range ch {
|
for f := range ch {
|
||||||
fmt.Printf("read f() from channel\n")
|
log.Println("read f() from channel")
|
||||||
f()
|
f.f()
|
||||||
}
|
}
|
||||||
|
log.Printf("andlabsGoroutine() END")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
functionChan = make(chan func(*sync.WaitGroup))
|
|
||||||
|
|
||||||
r := make(chan func() (int, error))
|
functionChan = make(chan *funcWait)
|
||||||
res, err = (<-r)()
|
|
||||||
|
// syntax to get the return values from a channel
|
||||||
|
// r := make(chan func() (int, error))
|
||||||
|
// afunc.res, afunc.err = (<-r)()
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go andlabsGoroutine(functionChan, &wg)
|
go andlabsGoroutine(functionChan, &wg)
|
||||||
|
|
||||||
for idx := 1; idx <= total; idx++ {
|
afunc.wgF = wg
|
||||||
generateNumbers(idx, functionChan, &wg)
|
afunc.f = func() {
|
||||||
|
log.Println("\tGOT INSIDE: running f() from inside the structure & inside the wait group goroutine")
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx := 1; idx <= 10; idx++ {
|
||||||
|
generateNumbers(idx, functionChan, &afunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(functionChan)
|
close(functionChan)
|
||||||
|
|
||||||
fmt.Println("Waiting for goroutines to finish...")
|
log.Println("Waiting for goroutines to finish...")
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("Done!")
|
log.Println("Done!")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue