in reply to Re: Tk Bind scalar
in thread Tk Bind scalar

Both those methods will do what I require which is great, however as soon as I try to share the reference the value on the traceVariable does not update, and on the Watch_variable the compiler complains 'attempt to free non-existent shared string'.

my $v = 0; share ($v); $mw->traceVariable(\$v, 'w' => [\&update_meter]); my $thr = threads->new(\&sub1); sub sub1{ while (1){ $v+= 1; sleep(1); print $v.":"; } }

thanks skywalker

Replies are listed 'Best First'.
Re^3: Tk Bind scalar
by jdporter (Paladin) on Feb 20, 2009 at 21:25 UTC

    Oh... Threads? I don't know. The conventional wisdom is, or at least used to be, that synchronous event-driven systems (e.g. GUIs like Tk) and asynchronous threading models don't really play well together, since the problem spaces they attempt to address are nearly the same, yet they take entirely different approaches. Maybe you could consider using POE, which (I've heard) integrates very nicely with Tk (see POE::Loop::Tk). That would get you out of the realm of threads, which is even more messy in Perl than in some other popular languages.

      If you take precautions, threads work fine in event-loop systems. As a matter of fact, in Glib, each thread can have its own independent loops.

      I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
        in Glib, each thread can have its own independent loops.

        Sure. But having a bunch of threads share a single "global" event loop is rather harder. That's why it's preferable (imo) to get a module to manage the mess (e.g. POE), rather than trying to reinvent the wheel. :-)

Re^3: Tk Bind scalar
by zentara (Cardinal) on Feb 20, 2009 at 21:20 UTC
    Well at a glance, shared vars for threads, need to be scalars.....I don't think it can be an object reference...... so maybe that would be a worthwhile feature of threads in Perl6?

    I know now in Tk, I need to have a timer running to read shared vars in a thread. A thread dosn't affect main's globals automatically.. An interesting trick I didn't try yet was communicating back thru filneo's......the threads all share those fd numbers.


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness