! gfortran -fopenmp -g -O3 -pedantic -Wall thralloc.F90
! OMP_NUM_THREADS=64 ./a.out

module test
  implicit none

  integer :: g_n
  real, allocatable :: g_a(:)

contains
  subroutine add(v)
    real, intent(in) :: v

    real, allocatable :: tmp(:)

    !$omp critical (add)
    if (.not. allocated(g_a)) then
       g_n = 1
       allocate(g_a(g_n))
    else
       allocate(tmp(g_n))
       tmp(1:g_n) = g_a(1:g_n)
       deallocate(g_a)
       allocate(g_a(g_n+1))
       g_a(1:g_n) = tmp(1:g_n)
       g_n = g_n + 1
    end if
    g_a(g_n) = v
    !$omp end critical (add)
  end subroutine add

  function get(i) result (g)
    integer, intent(in) :: i
    real :: g
    g = g_a(i)
  end function get
end module test

program main
  use test
  implicit none
  
  integer :: i, n
  logical, allocatable :: check(:)

  n = 100

  ! mimic add_warning
  !$omp parallel do
  do i = 1,n
     call add(real(i))
  end do
  
  ! print some sample entries, mimicking get_warning
  do i = 1,n,10
     print *,g_n,i,get(i)
  end do
  
  ! check that all expected entries are present
  allocate(check(n))
  check(:) = .false.
  do i = 1,n
     check(nint(get(i))) = .true.
  end do
  do i = 1,n
     if (.not. check(i)) print *,'entry',i,'not present'
  end do
end program main
