When you have such questions, then run your code with the race 
detector. https://golang.org/doc/articles/race_detector.html

Otherwise, you are only reading from the slice, there is no racing possible.

Also, your max function looks very weird and has a small bug:
https://play.golang.org/p/8K-0ze3RdDt

The slice calculations has bugs as well, e.g. try running with 64 
goroutines.

+ Egon

On Thursday, 19 April 2018 14:35:20 UTC+3, l vic wrote:
>
> I have a program that calculates max value in integer array by breaking 
> the array into number of slices and calculating max in every slice inside 
> of go-routine.
> Do I still need to lock/unlock each slice with mutex inside of go-routine? 
> The code seems to be working but are any apparent problems with it?
>
> package main
>
>
> import (
>
>     "fmt"
>
>     "os"
>
>     "strconv"
>
>     "sync"
>
> )
>
>
> //returns maximum number found in provided slice
>
> func maxInSlice(numbers []uint) (uint, error) {
>
>     if numbers == nil {
>
>         return 0, fmt.Errorf("nil  numbers")
>
>     }
>
>
>     var max uint = 0
>
>     for _, n := range numbers {
>
>         for _, m := range numbers {
>
>             if n > m {
>
>                 max = n
>
>             }
>
>         }
>
>     }
>
>
>     return max, nil
>
> }
>
>
> // finds  maximum  number  in  numbers  array  by   breaking  work  into  N  
> pieces
>
> // (where  N  is provided  as a command  line  argument)  and processing  the
>
> // pieces  in  parallel  goroutines func  main()
>
> func main() {
>
>     parallelism, _ := strconv.Atoi(os.Args[1])
>
>     fmt.Printf("ok,  i'll  use %d  goroutines\n", parallelism)
>
>
>     numbers := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
>
>     wg := sync.WaitGroup{}
>
>     mutex := sync.Mutex{}
>
>     var allMax uint = 0
>
>     perGoRoutine := len(numbers) / parallelism
>
>     fmt.Printf("perGoRoutine=%d\n", perGoRoutine)
>
>
>     for i := 0; i < parallelism; i++ {
>
>         wg.Add(1)
>
>         go func(i int) {
>
>             defer wg.Done()
>
>             fmt.Printf("==>index i = %d perGoRoutine in go func=%d\n", i, 
> perGoRoutine)
>
>             startIndex := perGoRoutine * i
>
>             endIndex := startIndex + perGoRoutine - 1
>
>             //include last element
>
>             if i == parallelism-1 {
>
>                 endIndex = len(numbers) - 1
>
>
>             }
>
>
>             fmt.Printf("startIndex=%d endIndex=%d\n", startIndex, endIndex)
>
>             sliceMax, err := maxInSlice(numbers[startIndex:endIndex])
>
>             mutex.Lock()
>
>             if err != nil {
>
>                 fmt.Printf("error  finding  max  for  slice  %d  to  %d, 
> skipping  this  slice:  %s\n", err)
>
>                 return
>
>             }
>
>
>             fmt.Printf("goroutine  %d  (slice  %d  to  %d)  found  max  
> %d\n", i, startIndex, endIndex, sliceMax)
>
>             if sliceMax > allMax {
>
>                 allMax = sliceMax
>
>             }
>
>             mutex.Unlock()
>
>
>         }(i)
>
>
>     }
>
>     wg.Wait()
>
>
>     fmt.Printf("maximum:  %d\n", allMax)
>
>
> }
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to