Gtk, perl and threads.



Hi all.

I'm currently hacking together a small utility for doing filetransfer
from ftp-servers through web-proxy.  Since this requires a bit of
parsing HTML, I decided to do this with perl.  I want it to be
graphical, so I use Gtk.

However, when I tried multithreading the program I ran into a bunch of
problems.  Having several threads accessing Gtk widgets resulted in
crashes.

So instead I came up with the following solution:
1. spawn a separate thread to contain the Gtk-functions
2. open pipes to between this thread and the main thread to communicate 
between them.
3. have the main thread spawning subthreads that do work like downloading.

I open the first thread using the "open(KID, "-|")" functionality 
described in perlipc pages. This works for sending signals from the 
gtk-thread to the main thread using a simple "while(<KID>)" loop.

I then try to open a pipe into the Gtk-thread using pipe, and connecting 
that to gdk->input. This seems to fail though. It looks like it receives 
a continuous stream of signals.

I connect the input-handler using the following function:
in main body:
pipe(PARENT, CHILD);
CHILD->autoflush(1);
(...)
in gtk-thread:
$ihMain = Gtk::Gdk->input_add( fileno(PARENT), ['read'], 
			       sub { &gtk_input_handler; } );

The entire source can be found at http://www.gimle.nu/~gobo/proxyftp.pl 
(although I don't quite think it is fit for sore eyes yet).

If someone knows what I'm doing wrong please let me know. 

Frank Ronny Larsen
-- 
"I'll carry your books, I'll carry a tune, I'll carry on, carry over, 
carry
forward, Cary Grant, cash & carry, Carry Me Back To Old Virginia, I'll 
even
Hara Kari if you show me how, but I will *not* carry a gun." 
                                                        -- Hawkeye, 
M*A*S*H







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