简体   繁体   English

popen管会减慢其他线程的速度

[英]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. 当在一个线程上执行同步popen()命令时,其他应用程序线程将大大减慢速度。 Thread with popen() execute ffmpeg , that generates high load. 使用popen()线程执行ffmpeg ,这会产生高负载。

Normally, other threads execution time is 0.0007 ms. 通常,其他线程的执行时间为0.0007 ms。 And when popen is used, some threads are increasing there execution time up to 14-20 seconds. 当使用popen时,某些线程将执行时间增加到14-20秒。

How to solve this problem? 如何解决这个问题呢?

System is FreeBSD 6.4 系统是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 这是无用的popen罐的新代码: 正确的代码-使用popen的非阻塞管道

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. fgets是阻塞读取,因此当上面的线程正在等待从管道读取数据时,其他线程将被阻塞。 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. 您将需要对文件描述符使用select / poll for,以在发出读取之前查看管道上是否有数据。 That way, you can preempt this thread, and let other threads run doing useful work. 这样,您可以抢占该线程,并让其他线程运行以进行有用的工作。

What is the relationship between the different threads? 不同线程之间是什么关系? If they depend on each other, meaning they send data back and forth, then if one thread goes slower, it makes sense that the others would as well. 如果它们相互依赖,这意味着它们来回发送数据,则如果一个线程变慢,则其他线程也将变慢。

Something else to consider is how the thread that is executing ffmpeg affects the rest of the system. 其他需要考虑的是执行ffmpeg的线程如何影响系统的其余部分。 For example, if its a single-core CPU and that particular thread is generating a high CPU load, then that would leave less cycles for the rest of the threads, thus slowing them down. 例如,如果它的单核CPU以及该特定线程正在产生较高的CPU负载,则其余线程的循环次数将减少,从而减慢它们的速度。 Granted, a change from 0.0007 ms to 14 - 20 seconds is indeed extreme! 当然,从0.0007毫秒更改为14-20秒确实是极端!

Are there any other resources shared (stdin, mutexes, etc) between the threads that the high load thread could be abusing (holding/locking too long), thus causing starvation for the other threads? 在高负载线程可能滥用(保持/锁定时间太长)的线程之间是否还有其他资源共享(stdin,互斥锁等),从而导致其他线程饿死?

Additionally, I would suggest profiling the application (or at least some of the threads) to see why it is so much slower. 另外,我建议对应用程序(或至少某些线程)进行性能分析,以了解为什么它这么慢。 Im almost positive that you'll find out that some threads are blocked waiting for a common resource, like a mutex or something similar. 我几乎肯定地说,您会发现某些线程在等待公共资源(例如互斥锁或类似对象)时被阻塞。

If this is linux, here are 2 Stack Overflow questions that may help: 如果是Linux,这里有两个堆栈溢出问题,可能会有所帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM