-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
weird interaction between sleep
function and Threads
#43952
Comments
I brought it up for discussion with @jpsamaroo and @vchuravy. My understanding is that, in julia> Threads.nthreads()
2
julia> ex1()
task slept for 4.99216890335083 seconds
julia> ex1()
task slept for 5.000109910964966 seconds
julia> Threads.@threads for _ in 1:1
ex1()
end
task slept for 1.002121925354004 seconds
julia> Threads.@threads for _ in 1:1
ex1()
end
task slept for 1.002121925354004 seconds Of course, this is a rather ugly hack. The best option usually is to put |
@tkf This is what |
FWIW, if you're referring to the slack question from earlier today, that was me who was asking :) |
Well, I was also asking this before :) But I figured out how threaded region works and that's why I tried I wonder if we want to have something like function prefer_io(f)
ccall(:jl_enter_threaded_region, Cvoid, ())
try
f()
finally
ccall(:jl_exit_threaded_region, Cvoid, ())
end
end to work around the case like the OP? It's ugly since it changes how the scheduler works globally but it at least works on arbitrary threads now. |
This issue was originally mentioned here.
gives the following result
it works as expected if the sleep function is replaced with a while loop
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).
The text was updated successfully, but these errors were encountered: