<ATOMIC_ADD Example Program>

Module stopping
  Use iso_fortran_env
  Integer(atomic_int_kind),Private :: nfinished[*] = 0
Contains
  Subroutine i_am_finished
    CALL atomic_add(nfinished[1],1)
  End Subroutine
  Logical Function all_finished()
    Integer(atomic_int_kind) res
    Call atomic_ref(res,nfinished[1])
    all_finished = res==Num_Images()
  End Function
End Module
Program atomic_add_example
  Use stopping
  Logical :: finished = .False.
  ! All images will execute until finished.
  ! There is no synchronisation between images.
  Do
    Call do_some_work(finished)
    If (finished) Then
      Call i_am_finished
      Exit
    End If
  End Do
  If (This_Image()==1) Then
    Do While (.Not.all_finished())
      Sync Memory
    End Do
    ! In principle we could do something with the results,
    ! but this is just a simple example.
    Print *,'All finished'
  End If
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

 All finished