Tickers in Fortran

Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.

program tickers
    use, intrinsic :: iso_fortran_env, only: int64
    implicit none

    integer(int64) :: tick_count, start_time, end_time, current_time
    real :: time_difference
    logical :: done

    ! Set up the ticker to run every 500 milliseconds
    tick_count = 0
    call system_clock(start_time)
    done = .false.

    do while (.not. done)
        call system_clock(current_time)
        time_difference = real(current_time - start_time) / 1000.0

        if (mod(int(time_difference * 2), 2) == 0) then
            tick_count = tick_count + 1
            print *, "Tick at", time_difference, "seconds"
        end if

        ! Stop after approximately 1600 milliseconds
        if (time_difference >= 1.6) then
            done = .true.
        end if
    end do

    print *, "Ticker stopped"
end program tickers

In this Fortran version, we simulate a ticker using a loop and the system_clock function. The program checks the time every iteration and prints a “tick” message every 500 milliseconds.

When we run this program, the ticker should tick approximately 3 times before we stop it.

$ gfortran tickers.f90 -o tickers
$ ./tickers
Tick at   0.500000000      seconds
Tick at    1.00000000      seconds
Tick at    1.50000000      seconds
Ticker stopped

Note that the exact timing may vary slightly due to system clock resolution and program execution time.

Fortran doesn’t have built-in support for tickers or timers like some other languages, so we’ve simulated the behavior using a loop and time checks. This approach doesn’t use separate threads or asynchronous operations, which are more complex to implement in standard Fortran.