Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

calling C sleep() using C binding

breakgate
Beginner
1,516 Views
Hello all,

I'm looking for a portable way to halt execution of my program for a time shorter than one second. I can't use Fortrans intrinsic sleep() function because it accepts only integer seconds.
I had thought to do this by calling the C function sleep using C binding. However, I keep getting a linker error: "unresolved external symbol _sleep referenced...".
I'm using Intel Visual Fortran Composer XE 2011.

See below for my code.
Thanks in advance for any help.

cheers,
Maarten

[fortran]module sleep_ms

interface
    function sleep_C(ms) bind(c,name="sleep")
        use iso_c_binding, only : c_int
        integer(c_int), value :: ms
        integer(c_int) :: sleep_C
    end function sleep_C
end interface

contains

    function sleep_ms(ms)
        use iso_c_binding, only: c_int
        implicit none
        integer, intent(in), value :: ms
        integer :: sleep_ms
        
        integer(c_int) :: ms_c
        
        ms_c = ms
        
        sleep_ms = sleep_C(ms_c)
        
    end function sleep_ms

end module sleep_ms[/fortran]

0 Kudos
1 Solution
TimP
Honored Contributor III
1,516 Views
You might check exactly how the sleep() linker symbol is spelled in the library you are using, by tools such as dumpbin.
There is no standard Fortran sleep function; you might find the ifort sleepqq function more convenient than the other legacy sleep function.

View solution in original post

0 Kudos
4 Replies
TimP
Honored Contributor III
1,517 Views
You might check exactly how the sleep() linker symbol is spelled in the library you are using, by tools such as dumpbin.
There is no standard Fortran sleep function; you might find the ifort sleepqq function more convenient than the other legacy sleep function.
0 Kudos
breakgate
Beginner
1,516 Views
Hi,

thanks for the fast answer. I had considered sleepqq but dismissed it because I assumed it was not portable to other compilers (specifically PGI Fortran). After some further searching I found that it *is* portable. In PGI Fortran it seems to be part of the module DFLIB, but this module seems also to be available in Intel Fortran.

cheers,
Maarten
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,516 Views
Maarten,

In IFPORT there is the funciton SLEEPQQ(milliseconds)

Jim Dempsey
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,516 Views
I see you already found SLEEPQQ...

When you program delays sometimes your interest is in the length of the delay while other times you are intersted in time intervals (IOW ~precisely every n milliseconds). In the latter case you also need to obtain the system time with sufficient resolution such that you can recompute the delay necessary to reach the desired time interval. Lookup SYSTEM_CLOCK as well as WIN32 API's which are also available.

Jim Dempsey
0 Kudos
Reply