Add tasks to queues of specific threads

Viewed 53

I'm trying to specify to run tasks on specific threads such that I can use two threads dedicated to a time consuming task that "put" to a channel and the others to processing that.

I’m stuck at how to assign a specific task to a specific thread. I thought I could use something like @spawnat but that does not seem to work. I wrote the code below to illustrate what I want to achieve

channel = Channel{Tuple{Int64, Int64}}(1000)

function stream()
    # won't actually use this later, but
    # easier to see what threads are used
     for new_item in channel
        println(new_item)
    end 
end

function cool_function(x::Int64)
    sleep(1)
    data = (Threads.threadid(), ~(x)+1)
    put!(channel, data)
end

function spawner(x::Array{Int64})
    for (i, number) in enumerate(x)
        if iseven(i)
            # Add to queue for thread X
            Threads.@spawn cool_function(i) 
        else
            # Add to queue for thread Y
            Threads.@spawn cool_function(i) 
        end
    end
end

@async stream()
spawner([1,2,3,4,5])

Any ideas on how to add tasks to a specific thread queue in spawner? :). Like "add to queue of thread1"

2 Answers

Probably not the greatest idea, but it's possible to do this:

task = Task(() -> cool_function(i))
tid = iseven(i) ? 1 : 2
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), task, tid - 1)
schedule(task)

Currently there is no an API at julia level for accessing the scheduler to assign a task to a specific thread,

but the package ThreadsPool.jl exposes the macro @tspawnat that allows direct thread assignment.

Related