0

I'm trying to find an alternative for fflush to clear the buffer in C++. I'm writing a C++ program to fork 3 children and print them in N loops but the O/P is not in correct order. Tried using cout<< flush and endl. Is there any other way to force the information to be displayed immediately? Thanks.

Nymeria
  • 5
  • 3
  • 4
    You have absolutely no guarantees, whatsoever, as to the order of output from concurrent running processes. It is up to your code to implement whatever mechanisms it needs in order to generate output from multiple processes in the correct order. If it were a single multi-threaded program then mutexes is the typical approach. With separate processes, a file locking-based approach is typically used. And flushing `std::cout` will be an integral part, that will also involve the implementation of whatever inter-process synchronization approach you will choose to implement. – Sam Varshavchik Sep 16 '18 at 04:02
  • Also, `fflush()` also does not provide any guaranteed ordering of output from concurrent processes. So Sam's comments are applicable to C as well. – Peter Sep 16 '18 at 05:38

1 Answers1

0

It's not optimal but you can use getchar(); cin.get();