Re: Adding text to a Gtk2::Label from another thread.



On Thu, 2004-01-01 at 10:44, Toby A Inkster wrote:
Hi all,

This is my first program using perl-Gtk2. Indeed it's my first program
using Gtk2 at all. And my first program using Perl's threading. So please
bear with me.

I have a function drawGui that (surprise, surprise) draws the GUI for
my program. I call this like:

      $guiThread = threads->create(\&drawGui);

using threads in conjunction with perl Gtk2 is, at the moment, not
really supported. the app will likely work correctly, but you are liable
to have issues at exit time during cleanup. (this is on the todo list,
but the solutions are not obvious.) 

it can be done if you're really careful how you go about things.
basically you have make sure to have all of your Gtk2 stuff in 1 of the
two threads. so you would have to split off the first thread before you
create any of your objects (which you seem to be doing.) 

the reason for this is that perl clones all of the existing
variables/objects when you spawn a thread, but doesn't do so in a way
that we (currently) can increment the refcount on the objects. so when
the first thread exits the (c level gtk) objects are destroyed and then
when the second either tries to use them or unref them you get core
dumps.

since you do seem to safely have things separated i would suggest
looking into using Thread::Queue (which you seem to be) to pass things
back and forth between the two threads rather than trying to access the
label directly. 

there are also other options that could avoid the use of threads
altogether, you may wish to look into those as there's apparently quite
a few dists out there with non-threaded perl on by default.

I have tried using a threads::shared style:

      my $statusfield : shared = Gtk2::Label->new;
      
But that doesn't seem to work at all.

perl doesn't support sharing of complex objects (supposedly yet) which
is what Gtk2 objects are and have to be. this is why you can't share
them using the method you are attempting to above. there's no way around
it until (if) perl itself doesn't something about it.

-rm




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