Tickers in Co-array Fortran

In Co-array Fortran, we can simulate tickers using a combination of the sleep subroutine from the iso_fortran_env module and a loop. While Co-array Fortran doesn’t have built-in tickers or channels like some other languages, we can create a similar effect.

program tickers
    use iso_fortran_env
    implicit none

    integer :: tick_count
    real :: start_time, current_time
    logical :: done

    tick_count = 0
    done = .false.
    call cpu_time(start_time)

    do while (.not. done)
        call sleep(0.5)  ! Sleep for 500 milliseconds
        tick_count = tick_count + 1
        call cpu_time(current_time)
        
        print *, "Tick at", current_time - start_time, "seconds"

        if (current_time - start_time >= 1.6) then
            done = .true.
        end if
    end do

    print *, "Ticker stopped"
end program tickers

In this program:

  1. We use the sleep subroutine to pause execution for 500 milliseconds (0.5 seconds) between each tick.

  2. We use a do while loop to simulate the continuous ticking until a condition is met.

  3. The cpu_time subroutine is used to get the current time and calculate the elapsed time.

  4. We print a message for each tick, showing the time elapsed since the start of the program.

  5. After approximately 1.6 seconds (slightly more due to execution time), we stop the ticker by setting done to .true..

When we run this program, it should tick 3 times before stopping, similar to the original example:

$ gfortran tickers.f90 -o tickers
$ ./tickers
Tick at   0.500999987 seconds
Tick at   1.00200002 seconds
Tick at   1.50300002 seconds
Ticker stopped

Note that the exact timing may vary slightly due to system-specific factors and the precision of the sleep subroutine.

This example demonstrates how to create a simple ticker-like behavior in Co-array Fortran. While it doesn’t use the parallel features of Co-array Fortran, it provides a similar functionality to the original example.