race condition finally removed, barrier in the loop ensures synchronized access to the buffer