-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Open
Labels
multithreadingBase.Threads and related functionalityBase.Threads and related functionality
Description
This issue was originally mentioned here.
function ex1()
task = @Threads.spawn begin
st = time()
sleep(1.0)
return time() - st
end
st2 = time()
while time() - st2 < 5 end
println("task slept for $(fetch(task)) seconds")
end
ex1()
ex1()
ex1()
ex1()
ex1()
ex1()gives the following result
task slept for 4.993403911590576 seconds
task slept for 5.000104904174805 seconds
task slept for 5.000086784362793 seconds
task slept for 5.000097990036011 seconds
task slept for 5.000089883804321 seconds
task slept for 5.000094175338745 seconds
it works as expected if the sleep function is replaced with a while loop
function ex2()
task = Threads.@spawn begin
st = time()
while time() - st < 1
end
return time() - st
end
st2 = time()
while time() - st2 < 5
end
return println("task slept for $(fetch(task)) seconds")
end
ex2()
ex2()task slept for 1.0 seconds
task slept for 1.0 seconds
I don't really know the under-the-hood details of Julia multithreading, but I would assume that there is some weird interaction with thread no.1 (the main julia thread) inside the sleep function, since it was designed for the async tasks; hence it “blocks”(yields back to the event loop, or whatever it is called in julia) the spawned task and doesn’t continue working until the main thread yields back, which doesn’t happen until it calls fetch (exactly after 5 seconds).
julia> VERSION
v"1.7.1"
maxstb and cmey
Metadata
Metadata
Assignees
Labels
multithreadingBase.Threads and related functionalityBase.Threads and related functionality