# Help optimizing bad multithreading code

**URL:** <https://discourse.julialang.org/t/help-optimizing-bad-multithreading-code/75099>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [January 24, 2022, 2:54am UTC](https://discourse.julialang.org/t/help-optimizing-bad-multithreading-code/75099 "2022-01-24T02:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaac.rubberducky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaac.rubberducky/32/36791_2.png) [@isaac.rubberducky](https://discourse.julialang.org/u/isaac.rubberducky)\
**Post date:** [January 24, 2022, 2:54am UTC](https://discourse.julialang.org/t/help-optimizing-bad-multithreading-code/75099/1 "2022-01-24T02:54:00Z")

</div>

```julia
function cleanExample(rootNode::Node)
    queue = Vector{Node}()
    push!(queue, rootNode)
    bestKnownSolution = 100

    queueLock = ReentrantLock()
    solutionLock = ReentrantLock()
    while !isempty(queue)
        Threads.@threads for it in 1:Threads.nthreads()
            nextNode = nothing
            begin
                lock(queueLock)
                try
                    if !isempty(queue)
                        nextNode = pop!(queue)
                    end
                finally
                    unlock(queueLock)
                end
            end
            if isnothing(nextNode)
                continue
            end
            
            newNode1, newNode2, bestFoundSolution = processNode!(nextNode)

            if !isnothing(newNode1)
                begin
                    lock(queueLock)
                    try
                        addToSortedQueue!(newNode1)
                    finally
                        unlock(queueLock)
                    end
                end
            end
            if !isnothing(newNode2)
                begin
                    lock(queueLock)
                    try
                        addToSortedQueue!(newNode2)
                    finally
                        unlock(queueLock)
                    end
                end
            end
            if isBetter(bestFoundSolution, bestKnownSolution)
                begin
                    lock(solutionLock)
                    try
                        bestKnownSolution = bestFoundSolution
                    finally
                        unlock(solutionLock)
                    end
                end
            end
        end
    end

    return bestKnownSolution
end

```

The intention of the above code is to solve a problem recursively by splitting the solution space into two completely different domain stores, and then processing each of the resulting spaces until each space can be closed. The code works as is, but it is clearly sub-optimal. The way it is written, each thread is assigned to a node, then when all the threads are done running, they are all each assigned again. This is as opposed to a thread being assigned to a new node as soon as one is available.

I’m looking for help correcting my code such that each thread is assigned a node as soon as it becomes available.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [January 24, 2022, 4:28pm UTC](https://discourse.julialang.org/t/help-optimizing-bad-multithreading-code/75099/2 "2022-01-24T16:28:14Z")

</div>

> [@isaac.rubberducky](#):
>
> each thread is assigned a node as soon as it becomes available

Producer-consumer:  
[https://docs.julialang.org/en/v1/manual/asynchronous-programming/#Communicating-with-Channels](https://docs.julialang.org/en/v1/manual/asynchronous-programming/#Communicating-with-Channels)

---

<div class="post-metadata">

**Author:** ![isaac.rubberducky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaac.rubberducky/32/36791_2.png) [@isaac.rubberducky](https://discourse.julialang.org/u/isaac.rubberducky)\
**Post date:** [January 25, 2022, 2:41pm UTC](https://discourse.julialang.org/t/help-optimizing-bad-multithreading-code/75099/3 "2022-01-25T14:41:00Z")

</div>

I’m not sure if the preference is that I leave posts up or not in this case but basically I made an earlier post that helped me refine my question further. Someone has posted a response to that earlier post that is also the solution to this one.

Link: [Help with setting up Distributed computing - #5 by albheim](https://discourse.julialang.org/t/help-with-setting-up-distributed-computing/75081/5)
