package main
import (
"sync"
"time"
"fmt"
)
//
// to use channel instead of "WaitGroup"
//
// in this case, slower than WaitGroup. Channel may be useful when it takes long processing time and less inforamtion size
//
func main() {
maxNumber := 1000*10000
var mu sync.Mutex
ch := make(chan int)
defer close(ch)
c := 2
oddCh := []int{}
tStart := time.Now()
tStop := time.Now()
for i := 2; i <= maxNumber; i++ {
go func() {
flag := true // if odd number, stay "true"
mu.Lock()
defer mu.Unlock()
for j :=2; j*j <= c ; j++ {
if c%j == 0{
flag = false
break
}
}
if flag == true{
ch <- c
}
c++
}()
}
for i:= 0; i < maxNumber; i++{ // set odd numbers to the slice
setBreak := false
select {
case p := <- ch:
oddCh = append(oddCh, p)
tStop = time.Now()
case <- time.After(time.Second): // to check last "ch" data
fmt.Println("Time Out!")
setBreak = true
}
if setBreak == true{
break
}
}
fmt.Println("len : ", len(oddCh))
el := tStop.Sub(tStart)
fmt.Println(el)
}
package main
import (
"sync"
"time"
"fmt"
)
func main() {
var wg sync.WaitGroup
var mu sync.Mutex
c := 2
odd := []int{}
tStart := time.Now()
for i := 2; i <= 10000*1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
flag := true // if odd number, stay "true"
mu.Lock()
defer mu.Unlock()
for j :=2; j*j <= c ; j++ {
if c%j == 0{
flag = false
break
}
}
if flag == true{
odd = append(odd, c)
}
c++
}()
}
wg.Wait()
tStop := time.Now()
fmt.Println(len(odd))
el := tStop.Sub(tStart)
fmt.Println(el)
}