I am trying to make a program in C++ to process a lot of packets in the fastest way possible. All the packets come from the standard should be read as fast as possible, sent to one thread from a pool to do the processing and then handled to an output thread that will write the packet to the standard output.
When you are using the standard input and output in C++, it's recommended that before any input or output you call to the std::ios_base::sync_with_stdio(false) function. In some environments this achieves a great speedup, although you should avoid using standard C functions for input/output after the call.
Well, this seems to work perfectly in a single thread. But as I have said my intention is using one thread for input, one for output and multiple threads for parallel processing. I've observed some problems with the output. This is the output thread (very simplified):
void PacketDispatcher::thread_process_output(OutputQueue& output_queue) {
std::vector<Packet> packet_list;
while(output_queue.get(packet_list)) {
for (const auto& packet: packet_list) {
std::cout << "Packet id = " << packet.id << "\n";
}
}
std::cout.flush();
}
If I used std::endl instead of "\n" there were less corruption, but std::endl forces a flush of the stream, affecting performance in this case (and the problem wasn't solved, only minimized).
That's the only point in the program using std::cout, but if I make the call to std::ios_base::sync_with_stdio(false) at the beggining of the program I get a noticeable speedup, but my output is corrupted always in some way:
Packet id = Packet id = 4
Packet id = 5
Packet id = 6
Packet id = 7
Packet id = 8
Packet id = 9
Packet id = 10
So, where is the problem? Isn't C++ able to do multithreading using fast standard input/output?