Re: GTK - Proper Way to Code Pipes



On Tue, 2006-12-05 at 22:27 +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.

I felt the same way, which is the main reason I posted to the list. I'm fairly new to GTK 
especially when it comes to complex issues like this.

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.

I'm pretty sure this is exactly what I've been seeing. Within my app I have a 'clicked' event 
attached to a button which basically starts all of my processing. Within this callback I call 
my main C++ routine which creates my objects and starts doing work. My app is multi-
threaded but I had such a headache trying with the threads_enter/leave and someone 
suggested using pipes and use g_io_add_watch to "watch" a pipe.


It would be alot easier for you to pass data to the main thread
from a child thread using g_idle_add(), just call g_idle_add() from
a thread and the callback will be executed in the main thread -
g_idle_add() is thread safe (you dont need the gdk lock to call
g_idle_add()).

If you then need to pass data you can use GAsyncQueue for that
I think (I never used it) - you can equally store the data you
want to pass on any data struct/list if you protect it properly
with a mutex.

Cheers,
                 -Tristan





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