Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrupted output with C++, cin, cout, threads and sync_with_stdio
    text
    copied!<p>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.</p> <p>When you are using the standard input and output in C++, it's recommended that before any input or output you call to the <a href="http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio">std::ios_base::sync_with_stdio(false)</a> function. In some environments this achieves a great speedup, although you should avoid using standard C functions for input/output after the call.</p> <p>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):</p> <pre><code>void PacketDispatcher::thread_process_output(OutputQueue&amp; output_queue) { std::vector&lt;Packet&gt; packet_list; while(output_queue.get(packet_list)) { for (const auto&amp; packet: packet_list) { std::cout &lt;&lt; "Packet id = " &lt;&lt; packet.id &lt;&lt; "\n"; } } std::cout.flush(); } </code></pre> <p>If I used <em>std::endl</em> instead of <em>"\n"</em> 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).</p> <p>That's the only point in the program using std::cout, but if I make the call to <a href="http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio">std::ios_base::sync_with_stdio(false)</a> at the beggining of the program I get a noticeable speedup, but my output is corrupted always in some way:</p> <pre><code>Packet id = Packet id = 4 Packet id = 5 Packet id = 6 Packet id = 7 Packet id = 8 Packet id = 9 Packet id = 10 </code></pre> <p>So, where is the problem? Isn't C++ able to do multithreading using fast standard input/output?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload