Re: GTK - Proper Way to Code Pipes
- From: Tristan Van Berkom <tvb gnome org>
- To: Christopher Bland <themanatuf juno com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: GTK - Proper Way to Code Pipes
- Date: Tue, 05 Dec 2006 16:58:17 -0500
On Tue, 2006-12-05 at 21:35 +0000, Christopher Bland wrote:
Based on suggestions from this mailing list I've implemented a pipe to
pass data from my other processes to the main GTK thread. I noticed
that when writing to the pipe there was a long delay before it was
handled. I'm assuming this is because it was a pending event.
Can someone please let me know if my code to "fix" this (for lack of a
better word) is correct? The code solves my issue and the pipe is read
immediately, I'm not sure if it is the proper way to do it.
[code]
... main processing ...
written = write(status_pipe[1], &var, strlen(var));
gtk_main_iteration();
... continue processing ...
[/code]
Is there a better way to do this? Thanks for any input.
Um, yes I'd say.
Maybe its just my personal opinion but I always prefer letting the main
loop do its thing and not hack around it by adding:
"while (events_pending) iteration_do();"
sprinkles here and there, I think its important to write your code in
short callbacks and return to the main loop as much as possible.
Pipes are pipes, in glib or otherwise you need an understanding of how
they work in order to give a precise forecast of what your program will
do.
Pipes block, for instance if you open a pipe for writing it will block
until a reader has opened the other end, writing to the pipe will block
until the reader reads the written bytes.
typically people will open (pipename, O_WRONLY | O_NONBLOCK) just to
open the file with out blocking, and then fcntl(fd...) to set it back
to blocking mode... then you might go ahead and select() the fd to know
if the pipe is ready to receive one byte of data without blocking
(maybe select()/write()ing one byte at a time until the buffer to write
is finished... always returning to the main loop when it would block).
Remember that this heavily depends on what you are going to do with
the pipe, do you want the reader to block in read() all the time ?
do you want the writer to block in write() and have the reader
test for a readable condition on the file descriptor before reading
a byte ? do you want to use nonblocking read/write operations on both
ends and just buffer the data in the pipe ? it can get complex.
Cheers,
-Tristan
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]