Starnge behaviour of atomicCounterIncrement

Hello,

I found a strange behaviour of atomicCounterIncrement. Both following compute shaders are started with glDispatchCompute(1, 1, 1);

#version 430 core

layout(local_size_x = 192) in;

layout(std430, binding = 1) buffer pathLengthBuffer
{
	uint[] pathLength; //6 elements
};

//initialized with 1
layout(binding = 0) uniform atomic_uint calculatedPathCounter;

void main()
{
	uint pathId;
	
	while ((pathId = atomicCounterIncrement(calculatedPathCounter)) < 5)
	{
		pathLength[pathId] = 1;
	}
}

Content of pathLength after execution:
Expected: {0, 1, 1, 1, 1, 0}
Is: {0, 1, 1, 1, 1, 0}

Now I have modifed this sahder a bit:

#version 430 core

layout(local_size_x = 192) in;

layout(std430, binding = 1) buffer pathLengthBuffer
{
	uint[] pathLength; //6 elements
};

//initialized with 1
layout(binding = 0) uniform atomic_uint calculatedPathCounter;

void main()
{
	uint pathId;
	if (gl_GlobalInvocationID.x == 5)
	{
		//Calculated only by 1 thread
		pathLength[0] = atomicCounterIncrement(calculatedPathCounter);
		pathLength[1] = atomicCounterIncrement(calculatedPathCounter);
		pathLength[2] = atomicCounterIncrement(calculatedPathCounter);
		pathLength[3] = atomicCounterIncrement(calculatedPathCounter);
	}
	while ((pathId = atomicCounterIncrement(calculatedPathCounter)) < 5)
	{
		pathLength[pathId] = 1;
	}
}

Content of pathLength after execution:
Expected: {1, 2, 3, 4, 0, 0}
Is: {1, 165, 166, 167, 1, 0}

Anyone an idea why result isn’t expected one?