Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17a2b6b

Browse files
committed
you're doing great
1 parent 3a16c17 commit 17a2b6b

File tree

3 files changed

+87
-0
lines changed
  • 22_go-routines/12_channels_pipeline/04_challenge-solution

3 files changed

+87
-0
lines changed
File renamed without changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"sync"
7+
)
8+
9+
const numFactorials = 100
10+
const rdLimit = 20
11+
12+
func main() {
13+
var w sync.WaitGroup
14+
w.Add(numFactorials)
15+
factorial(numFactorials, &w)
16+
w.Wait()
17+
18+
}
19+
20+
func factorial(n int, wmain *sync.WaitGroup) {
21+
var w sync.WaitGroup
22+
rand.Seed(42)
23+
24+
w.Add(n + 1)
25+
26+
for j := 1; j <= n; j++ {
27+
28+
go func() {
29+
f := rand.Intn(rdLimit)
30+
w.Wait()
31+
total := 1
32+
for i := f; i > 0; i-- {
33+
34+
total *= i
35+
}
36+
fmt.Println(f, total)
37+
(*wmain).Done()
38+
//out <- total
39+
40+
}()
41+
w.Done()
42+
}
43+
fmt.Println("All done with initialization")
44+
w.Done()
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
A student sent me the below note so I included the student's excellent solution to this project:
2+
3+
Todd, I reviewed your solution to 22_go-routines/04_challenge-solution in your Golang Programming class.
4+
5+
I believe your solution might not run 100 factorial computations concurrently and in parallel as the following statement will calculate the factorials sequentially, since it is receiving them - one by one - from the pipeline:
6+
out <- fact(n)
7+
8+
I used the sync package to create a solution that I believe will accomplish your goal. Let me know if I am missing something:
9+
10+
https://github.com/arsanjea/udemyTraining/blob/master/Exercises/exercise38.go
11+
12+
/////
13+
14+
# Definitions
15+
16+
## concurrency
17+
a design pattern
18+
go uses goroutines to create concurrent design patterns
19+
20+
## parallelism
21+
running code from a program on more than one cpu
22+
parallelism implies you have used concurrent design patterns
23+
24+
## sequentially
25+
one thing happening after another, in sequence
26+
27+
# here are my thoughts
28+
29+
The original solution uses concurrent design patterns. It uses two different goroutines. The [control flow](https://en.wikipedia.org/wiki/Control_flow) of the program is no longer a straight top-to-bottom sequence. Different goroutines have been launched.
30+
31+
The program may or may not run in parallel. If the program is run on a machine with two or more cpus, the program has the potential to run in parallel. Each of our three goroutines (the two goroutines we launched, and main) could be running on different cpu cores.
32+
33+
Jean-Marc Arsan is correct: the program IS still running sequentially. Even though calculations are occuring in different goroutines, and potentially on different CPU cores, the sequence in which they occur is still sequential.
34+
35+
goroutines allow synchronization of code.
36+
37+
In this original example, the code is synchronized.
38+
39+
Thank you, Jean-Marc, for your comment on this code!
40+
41+
I appreciate these discussions and hope the notes and your new code sample are helpful to everyone!
42+

0 commit comments

Comments
 (0)