How to kill thread?

You need to get the task handle to kill it, like that:

using .Threads

function hanging()
    println("I'm hanging on thread:", threadid())
    while true
        sleep(1)
    end
end

julia> hang = Threads.@spawn hanging();

julia> I'm hanging on thread:2

julia> hang
Task (runnable) @0x000000010f5698d0

julia> schedule(hang, ErrorException("stop"), error=true)
Task (failed) @0x000000010f5698d0
stop
try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at ./task.jl:611
....
4 Likes