Add OpenMP critical sections to avoid race conditions