I'm trying to print a set of Big Jobs ("1,2", "3,4", "5") in combination with Extra Jobs ("one", "two", "three") and using Goroutines and Workgroups to do so.
I am expecting the following output (not exactly in this order, but the internal Workgroups should finish first before the outer ones):
Big Job being done: 1,2
1_one
1_two
1_three
2_one
2_two
2_three
Big Job 1,2 is Done!
Big Job being done: 3,4
3_one
3_two
3_three
4_one
4_two
4_three
Big Job 3,4 is Done!
Big Job being done: 5
5_one
5_two
5_three
Big Job 5 is Done!
All Big Jobs are done!
Go Playground Link: https://play.golang.org/p/Hvbcmw06WY
However, when I run my code, I get the following output instead:
Big job 1,2 is Done!
Big job 3,4 is Done!
Big job 5 is Done!
Big Job being done: 5
5_three
Big Job being done: 1,2
1_three
Big Job being done: 3,4
3_three
5_one
5_two
1_one
1_two
3_one
3_two
fatal error: all goroutines are asleep - deadlock!
As you can see, the second portion of the Big Jobs (2
and 4
) were somehow "lost" inside the goroutines.
Also, the All Big Jobs are done!
message is never reached because all goroutines fall asleep somehow, even though each Waitgroup should have finished already (though the missing 2
and 4
portions might have something to do with it).
I also noticed that for some reason the Done
messages are being printed first (although I'm assuming that the Print functions are simply having trouble catching up with the Goroutines).
Here is the non-Goroutine version of what I want to accomplish: https://play.golang.org/p/zZpfyIbbn8
Any idea what I might be doing wrong?
jobWG.Wait()
is inside the for _, job := range jobs
loop when it should be outside.
Here is a fixed version https://play.golang.org/p/KNLS0y8xLg