mbauman
mbauman

Reputation: 31352

Is it possible to pin a task to a particular thread?

Julia's multithreading system is built atop tasks. I can create an asynchronous task that will always run on the thread that spawned it with an @async. I can create a task that can be run on any thread with a Threads.@spawn. Or I can split up an entire for loop across all available threads using Threads.@threads for ....

How can I put a task onto a particular thread of my choosing — that's not the one that created it?

Upvotes: 0

Views: 296

Answers (1)

carstenbauer
carstenbauer

Reputation: 10127

One way is to use ThreadPools.jl. It provides a @tspawnat macro that allows spawning a task on a specific thread.

Example: (taken from the package's README.md)

julia> t = @tspawnat 4 Threads.threadid()
Task (runnable) @0x0000000010743c70

julia> fetch(t)
4

The implementation of the macro can be found here.

Upvotes: 1

Related Questions