in reply to Annoying threads share problem!

Sorry, but you're out of luck. Sharing Objects of any kind between threads is not supported, and will not work at all. If you look in the threads::shared documentation under BUGS, you will see that blessed things cannot be shared.

You *might* be able to get around it by using freeze/thaw (in Storable) to save and restore your object (all Tk widgets are objects), but as I've spent many a frustrated moment attempting to do similar, I'll just tell you that its not a fun thing to do.

C.

Replies are listed 'Best First'.
Re^2: Annoying threads share problem!
by dave_the_m (Monsignor) on Oct 09, 2005 at 12:27 UTC
    you will see that blessed things cannot be shared
    No, shared things can't be (usefully) blessed, which is quite different. This is valid code:
    my $x = bless [], 'X'; share $x;
    The main problem with shared objects is that their destructor currently gets called multiple times, once per thread.

    Dave.

      Perhaps you'd comment on the disparity between that and this?

      use threads; use threads::shared;; my $x:shared = bless [], 'X'; Invalid value for shared scalar at (eval 4) line 1, <STDIN> line 2.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        Invalid value for shared scalar at (eval 4)
        You can only assign shared values to a shared variable. This works:
        my $obj = bless [], 'X'; share $obj; my $x:shared = $obj;

        Dave.

      I haven't gotten it reliably working either way, can you give a working example?

      C.

        I haven't gotten it reliably working either way, can you give a working example?
        Well, it depends rather on what exactly you are referring to as it, but
        use threads; use threads::shared; sub X::DESTROY { warn "X::DESTROY(@_)\n" } my $x = bless [], 'X'; share $x; $x->[0] = 1; sub f { $x->[0]++; warn "x->[0] = $x->[0]\n"; } threads->new(\&f)->join for 1..5; __END__ $ perl587t /tmp/p x->[0] = 2 X::DESTROY(X=ARRAY(0x9d8b218)) x->[0] = 3 X::DESTROY(X=ARRAY(0x9da0500)) x->[0] = 4 X::DESTROY(X=ARRAY(0x9da0500)) x->[0] = 5 X::DESTROY(X=ARRAY(0x9da0500)) x->[0] = 6 X::DESTROY(X=ARRAY(0x9da0500)) X::DESTROY(X=ARRAY(0x9cd3c30))

        Dave.

Re^2: Annoying threads share problem!
by Ace128 (Hermit) on Oct 09, 2005 at 12:00 UTC
    Come on! This is Perl! Must be more than one way doing it! ;) I'm curious on how it can work with JComboBox then... ?

    Maybe you have other ideas how to update the $full_pathMatchEntry widget?
      I'm curious on how it can work with JComboBox then... ?
      In Java all threads share a single address space. So although you can have a world of concurrency problems when writing threaded programs in Java, it's still perfectly possible to create complex objects in one thread and access them in another thread. And the AWT/Swing classes are largely thread-safe (for reading-type things anyway).