Help optimizing bad multithreading code

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.

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

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

1 Like