Search code examples
c++multithreadingpthreadspopen

popen pipe slows down other threads


I have problem with my multithread application. When at one thread executing synchronous popen() command - other application threads are slow down significantly. Thread with popen() execute ffmpeg, that generates high load.

Normally, other threads execution time is 0.0007 ms. And when popen is used, some threads are increasing there execution time up to 14-20 seconds.

How to solve this problem?

System is FreeBSD 6.4

    FILE *pipe;
    char buff[512];
    if ( !(pipe = popen( command.c_str(), "r")) )
    { // if pipe is NULL
        return false;
    }

    while ( fgets(buff, sizeof(buff), pipe) != NULL )
    {
        ptr_output->append(buff);
    }

here is new code of popen can that does NOT help: Correct Code - Non-blocking pipe with popen


Solution

  • fgets is a blocking read, so while the thread above is waiting for data to be read from the pipe, the other threads are blocked. You will want to use select/poll for with a file descriptor to see if you have data on the pipe before you issue a read. That way, you can preempt this thread, and let other threads run doing useful work.