Skip to content

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

@myrachanto

Description

@myrachanto

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    community mistakeMistakes proposed by the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions