[Solved] OpenMP – simplest accumulator in loops gives incorrect result


You’ve programmed a canonical data-race. All the threads in your program are contending to update the variable count and there are no guarantees about the order in which each thread reads, updates, then writes the values to the variable. Whatever you may believe C++ does not guarantee that ++ is applied atomically.

You should read on in your OpenMP tutorial to find out about shared (and private) variables, and also about reductions.

If you search around here on SO you should find similar questions, some of them with answers including code to show you how to do what you are trying to do.

solved OpenMP – simplest accumulator in loops gives incorrect result