There could have been a data race in your code with the usage of allMax
variable which is updated and checked by multiple goroutines. But you are
saved by the use of mutex. All shared variables, in general, are subject to
data races when there is a write involved. The local variables you create
and params passed by value to goroutines are not subject to data race. Your
usage of mutex is correct, but you can have it block only these lines --
mutex.Lock()
*if* sliceMax > allMax {
allMax = sliceMax
}
mutex.Unlock()
Now, whenever you have doubt, just remove mutex and run your code with
-race directive. Go runtime will provide details if any racy code is
detected.
Also, your slice range will omit last element, so do this -
endIndex = len(numbers)
instead of
endIndex = len(numbers) - 1
On Thursday, April 19, 2018 at 5:05:20 PM UTC+5:30, 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)
>
>
> }
>
>
>
>
>
--
*::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------
The contents of this e-mail and any attachments are confidential and
intended for the named recipient(s) only.E-mail transmission is not
guaranteed to be secure or error-free as information could be intercepted,
corrupted,lost, destroyed, arrive late or incomplete, or may contain
viruses in transmission. The e mail and its contents(with or without
referred errors) shall therefore not attach any liability on the originator
or redBus.com. Views or opinions, if any, presented in this email are
solely those of the author and may not necessarily reflect the views or
opinions of redBus.com. Any form of reproduction, dissemination, copying,
disclosure, modification,distribution and / or publication of this message
without the prior written consent of authorized representative of redbus.
<http://redbus.in/>com is strictly prohibited. If you have received this
email in error please delete it and notify the sender immediately.Before
opening any email and/or attachments, please check them for viruses and
other defects.*
--
*::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------
The contents of this e-mail and any attachments are confidential and
intended for the named recipient(s) only.E-mail transmission is not
guaranteed to be secure or error-free as information could be intercepted,
corrupted,lost, destroyed, arrive late or incomplete, or may contain
viruses in transmission. The e mail and its contents(with or without
referred errors) shall therefore not attach any liability on the originator
or redBus.com. Views or opinions, if any, presented in this email are
solely those of the author and may not necessarily reflect the views or
opinions of redBus.com. Any form of reproduction, dissemination, copying,
disclosure, modification,distribution and / or publication of this message
without the prior written consent of authorized representative of redbus.
<http://redbus.in/>com is strictly prohibited. If you have received this
email in error please delete it and notify the sender immediately.Before
opening any email and/or attachments, please check them for viruses and
other defects.*
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.