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

Re: gtk_timeout_add



The short answer is no and the long answer is yes.  You see, what
gtk_timeout_add does is add a timeout to the select() call that glib does.
A little more detail: your program communicates to the X server over a
pipe (could be a network socket, could be a unix socket).  When not doing
anything, glib (which contains the main loop) calls select() on that file
descriptor, as well as any that you specify with gdk_input_add().  One
feature of select is that it can time out.  That is what gtk_timeout_add
sets up.  While your program isn't in the select(), there is no timeout to
do, timeouts only begin once you enter the select() loop.  There are a few
options, one is to fork where you set up a pipe to communicate over and
add that to the select() using gdk_input_add().  Another is to use threads
and basically do the same thing.
	Personally, I use threads.  They're a bit harder, but not terribly
so since Gtk+ is now threadsafe.  Debugging them can be a bitch, but if
you design them so that you can make your app single-threaded for testing
purposes that's not too bad.  Past that, threads means that you have to
mutex anything that more than one thread might use.  The plus side is that
you can share resources without having to set up shared memory regions and
mutex them.  It largely depends on what you're doing.  If you're different
processes need to interact, I would use threads.  If they don't (like if
you're just spawning off a process to write something to disk) then
forking would probably be better.  Oh, another advantage of threads is
that when you use Gtk+ in thread-safe mode, all of your processes can do
gtk calls.  In forking cases only the main process can do them.
	Btw, if you just want a reliable timer, you might just spawn a
thread which sets up a timer (setitimer(), signal() to catch the produced
SIGALRMs) and then does the drawing.  You would probably have to do some
mutexing, but you wouldn't really loose anything in efficiency then you
would have had anyhow if you just set up a more or less global lock.
	-Chris

-- 
lansdoct@cs.alfred.edu
"If I had had more time I would have not written you at all." - Pascal
Linux Programs: http://cs.alfred.edu/~lansdoct/linux/
Linux - Get there. Today.
Evil Overlord Quote of the Day:
150.I will provide funding and research to develop tactical and strategic
weapons covering a full range of needs so my choices are not limited to
"hand to hand combat with swords" and "blow up the planet".




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