Re: GIOChannel & Intercepting stdout



On 08/26/2010 02:03 PM, Hrvoje Niksic wrote:

Exactly; it doesn't matter which thread is which, it's just important that they're in different threads or processes, so they don't deadlock. For example, it wouldn't be enough to place the output sink in the main loop of a single-threaded program, because it wouldn't get a chance to run during a printf(). This means that you must never print anything to stdout from the thread that runs the mainloop; if you do (and the size of the output exceeds the pipe's buffer, which is fairly small), that thread will deadlock deep inside stdio. This can be a nasty gotcha for someone that maintains the code later, so a big fat comment might be in order.

Is the buffer really that small? I made a simple test program (below) to check this out, and it seems that writes up to 64K work without blocking on my Ubuntu system. Of course, it is still ugly (and non-portable) to depend on this.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int i,p[2];
    char buf[65536];
    i = pipe(p);
    printf("%d\n",i);
    i = write(p[1],buf,sizeof(buf));
    printf("%d\n",i);
    return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]