|Really for the OP but I will put here to follow the discussion
Someone is on a multicore processor and the streams which are buffering are on different cores with resource locks
This is generally not guaranteed to give the expected order
Simply put two independent streams are not guaranteed to synchronize without you forcing the issue.
In the above Expect1 will always be before Expect3 but Expect2 can be before, in the middle or after those two lines.
sync_with_stdio() if implemented will work but it implies a heavy overhead on a many multicore situation which is why it might be off. I think it is supposed to default on with some c++ versions (but on multicore it is often violated because it is horrific) you would need to check with your compiler.
The simple answer is stop using perror and simply just use one stream aka printf/cout, flush after each print or put lock primitives in the order to guarantee the order.
It is easily avoidable ... so avoid it
In vino veritas
modified 12-Mar-19 1:36am.