Example sleeptimes
Run:
bagof([Sleep,Delta] ,T_begin^T_end^(member(Sleep,[-1,0,0.5,1,5]), get_time(T_begin), sleep(Sleep), get_time(T_end), Delta is max(0,T_end-T_begin)) ,Bag).
After a few seconds you get something like:
Bag = [[-1, 1.9073486328125e-6], [0, 5.9604644775390625e-5], [0.5, 0.5000655651092529], [1, 1.0000708103179932], [5, 5.000069856643677]].
Missed opportunity!
This is a miss for the use of is
. One could have done this:
ActuallySlept is sleep(WantedSleep).
or at least the non-logical predicate:
sleep(WantedSleep,ActuallySlept).
Also, what about sleeping until some point?
All times in Unix time (see get_time/1).
sleep_until(When) :- get_time(Now), Delay is (When - Now), sleep(Delay). % if Delay <= 0, return immediately
Is sleep interruptible?
Apparently not. Even not through inter-thread signaling (I haven't tested this)
Documentation says;
On most non-realtime operating systems we can only ensure execution is suspended for at least Time seconds.
Compare with Java's Thread.sleep()
which sleeps at least the specified time, but may be interrupted early. It must be interruptible because some other thread could tell your thread you to shut down: https://www.javaspecialists.eu/archive/Issue056.html
public void run() { while (true) { System.out.print("."); System.out.flush(); try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); // very important break; } } System.out.println("Shutting down thread"); }