Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/website: Effective Go: bug in code example about concurrency #47357

Closed
vitaly-zdanevich opened this issue Jul 23, 2021 · 1 comment
Closed

Comments

@vitaly-zdanevich
Copy link
Contributor

vitaly-zdanevich commented Jul 23, 2021

Here.

const numCPU = 4 // number of CPU cores

func (v Vector) DoAll(u Vector) {
    c := make(chan int, numCPU)  // Buffering optional but sensible.
    for i := 0; i < numCPU; i++ {
        go v.DoSome(i*len(v)/numCPU, (i+1)*len(v)/numCPU, u, c)
    }
    // Drain the channel.
    for i := 0; i < numCPU; i++ {
        <-c    // wait for one task to complete
    }
    // All done.
}

If len(v) is 10 doSome will receive
i = 0, n = 2
i = 2, n = 5
i = 5, n = 7
i = 7, n = 10

And because in doSome we have i < n we drop 10.

@gopherbot gopherbot added this to the Unreleased milestone Jul 23, 2021
@vitaly-zdanevich
Copy link
Contributor Author

Sorry I was wrong - we count from 0.

@golang golang locked and limited conversation to collaborators Jul 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants