Re: Proper way to provide gtk+ app with asynchronous data?



I've found that with the third thread I can do a very nice thing: to filter data so that (my "data" is consequent tool positions, i. e. vectors): if during drawing of the last vector there appeared, say, 10 more vectors to draw, I can throw away first 9 and draw only the last, the most fresh one. In my case it would be helpful, especially if it'll be drawn in slow 3D.

So, 3 threads.

Thanks all for help!

NavEcos wrote:

You can use a pipe and use 3 threads.

Use one thread for the "main" gtk loop - this will the thread the calls
gtk_main()
Use one thread to acquire data and send the data through a pipe
Use one thread to do the updates to the screen asynchrnously - it will get
its data via a pipe.

It's been a while since I've use pipes, but as I recall (dimly) it's pretty
straighforward, just do a "man 2 pipe"

Another note about using threads with gtk - do NOT do this in your
asynchronous thread:

for (;;)
{
 gdk_threads_enter()
 // wait for data (!)
 // update screen
 gdk_threads_leave();
}

do this instead

for (;;)
{
 // wait for data (don't block between gdk_threads_enter & leave)
 gdk_threads_enter()
 // update screen
 gdk_threads_leave();
}

If you do the first one, you'll lock up your application update while data
is waiting to be received.

Do not waste a lot of time trying to do gdk_threads_enter() and ...leave()
in lots of places to minimize the time spent in the gdk_threads_enter() and
...leave() areas - do NOT think of it as a critical section another words.
Keep it simple!

Another fun thing - if you try to set a breakpoint in gdb where
gdk_threads_enter() has been entered, it will lock up your entire screen
sometimes.  It pays to have two X11 sessions or 2 machines: OR better yet:
you can get around this though using Xvnc - use a virtual X11 display.  Xvnc
is part of TightVNC, do a man on vncviewer and vncserver.  There are
vncviewers available for Windows, and let me tell you, it's great.

Good luck, it's not a lot of fun to deal with threads in my opinion with
GTK.

-Rich

P.S. If you've not played with it, try fooling around with TightVNC a bit.
It's a wonderful little program and extremely useful, and the best kept
secret of Linux in my opinion.  With it, we run 5 users on a single machine,
and use dumb terminals with either Knoppix or Windoze to connect.  It
greatly reduces our IT headaches, and saved us lots of money.
Well, when it'll be about money for me, I'll think of it, of cource.




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