将任务添加到特定线程的队列中



我试图指定在特定线程上运行任务,这样我就可以使用两个线程专门用于一个耗时的任务;放入";到一个通道,而其他通道处理该通道。

我一直纠结于如何将特定任务分配给特定线程。我想我可以使用类似@spawnat的东西,但似乎不起作用。我写了下面的代码来说明我想要实现

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])

关于如何在spawner中将任务添加到特定线程队列,有什么想法吗?:(。喜欢";添加到线程1的队列";

目前没有julia级别的API来访问调度程序,将任务分配给特定线程

但是包ThreadPool.jl公开了允许直接线程分配的宏CCD_ 3。

可能不是最好的主意,但可以做到这一点:

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