When Perl lacks threads support, just minor changes for the 2nd demonstration and voila.

use threads; use threads::shared; ... my $data_in:shared = 0; my $data_ret:shared = '----'; my $button_control:shared = 0; my $go_control:shared = 0; my $die_control:shared = 0; ... my $thr = threads->create( \&worker ); ...

To this...

use MCE::Hobo; use MCE::Shared; ... tie my $data_in, 'MCE::Shared', 0; tie my $data_ret, 'MCE::Shared', '----'; tie my $button_control, 'MCE::Shared', 0; tie my $go_control, 'MCE::Shared', 0; tie my $die_control, 'MCE::Shared', 0; ... my $thr = MCE::Hobo->create( \&worker ); ...

Script with changes applied.

#!/usr/bin/perl # Use MCE::Hobo for Perl lacking threads support. # Based on zentara's 2nd demonstration found here. # http://www.perlmonks.org/?node_id=1188686 use warnings; use strict; use MCE::Hobo; use MCE::Shared; # declare, share then assign tie my $data_in, 'MCE::Shared', 0; tie my $data_ret, 'MCE::Shared', '----'; tie my $button_control, 'MCE::Shared', 0; tie my $go_control, 'MCE::Shared', 0; # create hobo before any tk code is called my $hobo = MCE::Hobo->create( \&worker ); use Tk; my $mw = MainWindow->new(); my $val = '----'; my $label = $mw->Label( -width => 50, -textvariable => \$val )->pack( +); my $button = $mw->Button( -text => 'Start', -command => \&start )->pac +k(); # you need a timer to read the shared var in hobo my $timer = $mw->repeat( 10, sub { $val = $data_ret } ); my $timer1 = $mw->repeat( 10, sub { if ($button_control) { $button->configure( -text=> "Next" ); $button_control = 0; } }); $mw->protocol( WM_DELETE_WINDOW => sub { $timer->cancel; $timer1->cancel; $hobo->exit->join; exit; }); MainLoop; sub start { $button->configure( -text=> "----" ); $go_control = 1; # pass some new data in $data_in = int rand(20); } # no Tk code in hobo sub worker { my $count; while (1) { # wait for $go_control if ($go_control) { print "incoming $data_in\n"; print ++$count, "\n"; if ($count >= $data_in) { $go_control = 0; $data_ret = $count; $button_control = 1; } $data_ret = $count; select(undef,undef,undef,.25); } # sleep until awakened else { $count = 0; select(undef,undef,undef,.25); } } return; }


In reply to Re^2: how to fork & join in tk? by marioroy
in thread how to fork & join in tk? by redss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.