Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Process continuously read output
From: degski (degski_at_[hidden])
Date: 2018-05-16 05:18:57


On 16 May 2018 at 05:11, Surya Kiran Gullapalli via Boost-users <
boost-users_at_[hidden]> wrote:

> Hi,
> I'm trying to read outputs/logs from different processes and display them
> in a GUI. The processes will be running for long time and produce huge
> output. I'm planning to stream the output from those processes and display
> them according to my needs. All the while allow my gui application to take
> user inputs and perform other actions.
>
> What I've done here is, from main thread launch two threads for each
> process. One for launching the process and another for reading output from
> the process.
>
> This is the solution I've come up thus far.
>
> // Process Class
> class MyProcess {
> namespace bp = boost::process;
> boost::asio::io_service mService; // member variable of the class
> bp::ipstream mStream // member variable of the class
> std::thread mProcessThread, mReaderThread // member variables of the class.
>
> public void launch();
> };
>
> void
> MyProcess::launch()
> {
> mReaderThread = std::thread([&](){
> std::string line;
> while(getline(mStream, line)) {
> std::cout << line << std::endl;
> }
> });
>
> mProcessThread = std::thread([&]() {
> auto c = boost::child ("/path/of/executable", bp::std_out > mStream,
> mService);
>
> mService.run();
> mStream.pipe().close();
> }
> }
>
>
> // Main Gui class
> class MyGui
> {
> MyProcess process;
> void launchProcess();
> }
>
> MyGui::launchProcess()
> {
> process.launch();
> doSomethingElse();
> }
>
> The program is working as expected so far. But I'm not sure if this is the
> correct solution. Please let me know if there's any
> alternative/better/correct solution
>

There is a place to put/ask this kind of questions:
https://codereview.stackexchange.com/

degski

-- 
*"If something cannot go on forever, it will stop" - Herbert Stein*


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net