Akobold
Akobold

Reputation: 946

Capture the output of a process in multi-threaded c++

My requirements are simple: start a process, wait for it to finish, then capture and process it's output.

For the longest time I've been using the following:

struct line : public std∷string {
    friend std∷istream& operator>> (std∷istream &is, line &l) {
        return std∷getline(is, l);
    }
};

void capture(std::vector<std::string> &output, const char *command)
{
    output.clear();
    FILE *f = popen(command, "r");
    if(f) {
        __gnu_cxx::stdio_filebuf<char> fb(f, ios∷in) ;
        std::istream fs(&fb);
        std::istream_iterator<line> start(fs), end;
        output.insert(output.end(), start, end);
        pclose(f);
    }
}

And it works really well on single threaded programs.

However, if I call this function from inside a thread, sometimes the popen() call hangs and never return.

So, as a proof-of-concept I replaced the function for this ugly hack:

void capture(std::vector<std::string> &output, const char *command)
{
    output.clear();
    std::string c = std::string(command) + " > /tmp/out.txt";
    ::system(c.c_str());
    ifstream fs("/tmp/out.txt", std::ios::in);
    output.insert(output.end(), istream_iterator<line>(fs), istream_iterator<line>());
    unlink("/tmp/out.txt");
}

It's ugly but works, however it kept me wondering what would be the proper way to capture a process output on a multi-threaded program.

The program runs on linux in a embedded powerquiccII processor.

Upvotes: 0

Views: 1712

Answers (1)

John Zwinck
John Zwinck

Reputation: 249502

See this: popen - locks or not thread safe? and other references do not seem conclusive that popen() needs to be thread-safe, so perhaps since you are using a less-popular platform, your implementation is not. Any chance you can view the source code of the implementation for your platform?

Otherwise, consider creating a new process and waiting upon it. Or hey, stick with the silly system() hack, but do handle its return code!

Upvotes: 2

Related Questions