Logo

Programming-Idioms

  • Groovy
  • Fortran

Idiom #45 Pause execution for 5 seconds

Sleep for 5 seconds in current thread, before proceeding with the next instructions.

The pedestrian walks, then stays still for 5 seconds, then resumes walking
module M_time
contains
subroutine system_sleep(wait)
use,intrinsic :: iso_c_binding, only: c_int
integer,intent(in) :: wait
integer(kind=c_int):: waited
interface
 function c_usleep(msecs) bind (C,name="usleep")
  import
  integer(c_int) :: c_usleep
  integer(c_int),intent(in),VALUE :: msecs
 end function c_usleep
end interface
 if(wait.gt.0)then
  waited=c_usleep(int(wait,kind=c_int))
 endif
end subroutine system_sleep
end module M_time
program x
use M_time
call system_sleep(5000000)
end  

Fortran does not have an intrinsic to pause for a specified time period, but it is relatively easy to create an interface to a POSIX C routine
delay 5.0;

New implementation...
< >
programming-idioms.org