<ATOMIC_FETCH_ADD Example Program>

Program atomic_fetch_add_example
  Use iso_fortran_env
  Implicit None
  Integer(atomic_int_kind) :: nexttask[*] = 1, task
  Integer i
  Integer :: task_done_by(1000)[*] = 0
  Do
    Call atomic_fetch_add(nexttask[1],1,task)
    If (task>1000) Exit
    Call do_task(task)
  End Do
  Sync All
  If (This_Image()==1) Then
    Do i=1,Num_Images()
      Print *,Count(task_done_by==i),'tasks done by image',i
    End Do
  End If
Contains
  Subroutine do_task(num)
    Integer(atomic_int_kind),Intent(In) :: num
    ! This is where we would do the work, if this were not an example.
    task_done_by(num)[1] = This_Image()
  End Subroutine
End Program

■ Execution Results

(Possible results with four images.)
 707 tasks done by image 1
 124 tasks done by image 2
 142 tasks done by image 3
  27 tasks done by image 4