Boost C++ Libraries: Ticket #13103: Documentation: Line-by-line I/O example should use asio https://svn.boost.org/trac10/ticket/13103 <p> The boost::process Tutorial needs some improvements: </p> <p> When following the example "<a href="http://www.boost.org/doc/libs/1_64_0/doc/html/boost_process/tutorial.html#boost_process.tutorial.io">Synchronous I/O</a>" the output may be incomplete. Actually, this is even stated in the <a href="http://www.boost.org/doc/libs/1_64_0/doc/html/boost_process/faq.html#boost_process.faq.closep">FAQ</a>: "But, since pipes are buffered, you might get incomplete data if you do this: It is therefore highly recommended that you use the asynchronous api if you are not absolutely sure how the output will look." </p> <p> Not retrieving all data is not something to be generally expected; therefore this should be stated clearly in the tutorial's example. A new example should provide a solution to the problem. (Would have saved me hours of work!) </p> <p> Or even better, since the asynchronous api is highly recommended anyway, why not deprecate synchronous IO and replace the example completely? </p> <p> The new/additional example could look somewhat like this: </p> <pre class="wiki">#include &lt;boost/process.hpp&gt; #include &lt;iostream&gt; #include &lt;functional&gt; #include &lt;sstream&gt; // capture process output line by line int main() { const std::string file = "file"; namespace bp = boost::process; bp::opstream os; struct IoData { bp::async_pipe ap; // Pipe to be used by the stream std::vector&lt;char&gt; buffer; std::mutex streamMutex; std::string keepline; // Store incomplete data std::function&lt;void(const std::string&amp;)&gt; callback; IoData(boost::asio::io_service&amp; ios_, std::function&lt;void(const std::string&amp;)&gt; callback_) : ap(ios_) , buffer(512) // Set arbitrary buffer size here! , callback(callback_) { } }; // set up async io boost::asio::io_service ios; // Prepare capturing both stdout and stderr IoData err(ios, [](const std::string&amp; s){ std::cout &lt;&lt; "cerr: " &lt;&lt; s &lt;&lt;std::endl; }); IoData out(ios, [](const std::string&amp; s){ std::cout &lt;&lt; "cout: " &lt;&lt; s &lt;&lt;std::endl; }); bp::child process(bp::search_path("nm"), file, bp::std_out &gt; out.ap, bp::std_err &gt; err.ap, ios); std::function&lt;void(IoData&amp;)&gt; beginRead = [&amp;](IoData&amp; io) { io.ap.async_read_some(boost::asio::buffer(io.buffer, io.buffer.size()), [&amp;](const boost::system::error_code &amp;ec, std::size_t size) { std::lock_guard&lt;std::mutex&gt; guard(io.streamMutex); std::string str(io.buffer.data(), size); std::stringstream stream(io.keepline + str); std::string line; while (std::getline(stream, line)) { io.callback(line); } if (stream.eof()) { io.keepline = line; } else { io.keepline = ""; } if (ec.value() != 0) { return; } beginRead(io); }); }; beginRead(err); beginRead(out); ios.run(); process.wait(); }; </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13103 Trac 1.4.3