<ATOMIC_DEFINE Example Program>

Module stopping
  Use iso_fortran_env
  Logical(atomic_logical_kind),Private :: stop_flag[*] = .False.
Contains
  Subroutine make_it_stop
    CALL atomic_define(stop_flag[1],.TRUE._atomic_logical_kind)
  End Subroutine
  Logical(atomic_logical_kind) Function please_stop()
    Call atomic_ref(please_stop,stop_flag[1])
  End Function
End Module
Program atomic_define_example
  Use stopping
  Logical :: finished = .False.
  ! All images will execute until one decides we are finished.
  ! There is no synchronisation between images.
  Do
    Call do_some_work(finished)
    If (finished) Call make_it_stop
    ! All images will (eventually) exit if anyone calls "make_it_stop".
    If (please_stop()) Exit
  End Do
  Print *,'Stopped'
Contains
  Subroutine do_some_work(done)
    Logical,Intent(Out) :: done
    Real x
    ! Because this is an example, we don't actually do any work.
    ! We just randomly (1%) set the done flag.
    Call random_number(x)
    done = x>0.99
  End Subroutine
End Program

■ Execution Results

 Stopped