I was pretty surprised, so checked the source which is basically wait(Timer(sec)), so it’s allocating a mutable struct for that Timer. Is there a more efficient alternative to that to sleep for a very short time?
I found a reference to usleep but I’m on Windows so that’s not an option for me.
I need a thread to sleep briefly (milliseconds) waiting for more work to come in. I think using yield instead might work for me, but it would be nice to have an efficient sleep option (or improve my understanding if it’s not possible).
What kind of sleep is Libc.systemsleep? The regular (allocating) version has the benefit of allowing other julia tasks to run in the background, since it’s yielding to the scheduler.
Thanks for the mention of Libc.systemsleep. Unfortunately, the doc states: “This function does not yield to Julia’s scheduler and therefore blocks the Julia thread that it is running on for the duration of the sleep time.” This sounds like it won’t be useful. I thought I would test it. I ran the code below and it does hang for 4 seconds (when running Julia with only one thread), so… it appears it won’t do what I need (if the test is correct). The output for duration seems a little odd to me, though.
start 1
end 1
start 1
end 1
duration: 0.004000186920166016
function slong()
println("start $(Threads.threadid())")
Libc.systemsleep(2.0)
println("end $(Threads.threadid())") )
end
function run()
t1 = time()
Threads.@spawn slong()
yield()
Threads.@spawn slong()
yield()
t2 = time()
println("duration: $(t2 - t1)")
end