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

https://100go.co/56-concurrency-faster/ - Use of Semaphores #93

Open
myrachanto opened this issue Jun 10, 2024 · 0 comments
Open

https://100go.co/56-concurrency-faster/ - Use of Semaphores #93

myrachanto opened this issue Jun 10, 2024 · 0 comments
Labels
community mistake Mistakes proposed by the community

Comments

@myrachanto
Copy link

Use of Semaphores

First of all, let me say that this is amazing work correcting 100 Golang code mistakes while writing clean code. Thank you for your hard work. I am learning a lot.

Second, it is not an issue just an observation - wouldn't semaphores be the best solution for making concurrency more productive? The solution is to teach the best there is to teach about Golang right?

`const max = 2048 // Defines the threshold

func parallelMergesortV2(s []int) {

if len(s) <= 1 {

    return

}



if len(s) <= max {

    sequentialMergesort(s) // Calls our initial sequential version

} else { // If bigger than the threshold, keeps the parallel version

    middle := len(s) / 2



    var wg sync.WaitGroup

    wg.Add(2)



    go func() {

        defer wg.Done()

        parallelMergesortV2(s[:middle])

    }()



    go func() {

        defer wg.Done()

        parallelMergesortV2(s[middle:])

    }()



    wg.Wait()

    merge(s, middle)

}

}`

Solution
I would recommend the use of semaphores

One or multiple solutions to avoid the mistake.

@myrachanto myrachanto added the community mistake Mistakes proposed by the community label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community mistake Mistakes proposed by the community
Projects
None yet
Development

No branches or pull requests

1 participant