popen管减慢其他线程 [英] popen pipe slows down other threads
问题描述
我的多线程应用程序有问题。当在一个线程执行同步 popen()
命令 - 其他应用程序线程显着减慢。用 popen()
的线程执行 ffmpeg
,会产生高负载。
$ b b
通常,其他线程的执行时间为0.0007 ms。当使用 popen
时,某些线程的执行时间增加到14-20秒。
如何解决这个问题?
系统是FreeBSD 6.4
FILE * pipe;
char buff [512];
if(!(pipe = popen(command.c_str(),r)))
{//如果pipe为NULL
return false;
}
while(fgets(buff,sizeof(buff),pipe)!= NULL)
{
ptr_output-&
}
这里是popen的新代码,不会帮助:正确的代码 - 带有popen的非阻塞管道
< fgets是一个阻塞读取,所以当上面的线程正在等待从管道读取数据时,其他线程被阻塞。您将希望对文件描述符使用select / poll,以查看在发出读取之前是否在管道上有数据。这样,你可以抢占这个线程,并让其他线程运行做有用的工作。
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
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.
这篇关于popen管减慢其他线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!