Updated: Threads require a signal handler when requested to terminate.

The threads module along with MCE::Shared is another possibility. Like before using MCE::Hobo, shared data constructed with MCE::Shared resides under the shared-manager. MCE::Shared spawns a thread versus a child process when threads is present.

example_t1.pl

#!/usr/bin/env perl use strict; use warnings; use stfl; use threads; use MCE::Shared; use Time::HiRes qw/sleep/; my $count1 = MCE::Shared->scalar(10000); my $count2 = MCE::Shared->scalar(20000); my $count3 = MCE::Shared->scalar(30000); my $layout; { open my $fh, "<", "example.stfl" or die "open error 'example.stfl': $!"; local $/; $layout = <$fh>; } my $f = stfl::create($layout); my $s = 0; sub bg_start { unless ($s) { async { local $SIG{'QUIT'} = sub { threads->exit() }; sleep(0.9), $count1->incr() while 1; }; async { local $SIG{'QUIT'} = sub { threads->exit() }; sleep(0.6), $count2->incr() while 1; }; async { local $SIG{'QUIT'} = sub { threads->exit() }; sleep(0.3), $count3->incr() while 1; }; $s = 1; } } sub bg_stop { if ($s) { # Signal the thread to terminate, and then detach # it so that it will get cleaned up automatically. $_->kill('QUIT')->detach() for threads->list(); $s = 0; } } $f->set('helpmsg', '[ ESC = exit | F1 = start | F2 = stop ]'); bg_start(); while (1) { my $event = $f->run(50); if ($s) { # must stringify in case numeric value $f->set('text 1', ''.$count1->get()); $f->set('text 2', ''.$count2->get()); $f->set('text 3', ''.$count3->get()); } next unless (defined $event); bg_start() if $event eq 'F1'; bg_stop() if $event eq 'F2'; last if $event eq 'ESC'; } bg_stop();

example_t2.pl

#!/usr/bin/env perl use strict; use warnings; use stfl; use threads; use MCE::Shared; use Time::HiRes qw/sleep/; my $q = MCE::Shared->queue(); # Start the shared-manager manually. # https://metacpan.org/pod/MCE::Shared::Queue#LIMITATIONS MCE::Shared->start() unless $INC{'IO::FDPass.pm'}; my $layout; { open my $fh, "<", "example.stfl" or die "open error 'example.stfl': $!"; local $/; $layout = <$fh>; } my $f = stfl::create($layout); my $s = 0; async { my $c = 10000; sleep(0.9), $q->enqueue([ 'text 1', ++$c ]) while 1; }; async { my $c = 20000; sleep(0.6), $q->enqueue([ 'text 2', ++$c ]) while 1; }; async { my $c = 30000; sleep(0.3), $q->enqueue([ 'text 3', ++$c ]) while 1; }; $f->set('helpmsg', '[ ESC = exit ]'); while (1) { my $event = $f->run(50); foreach my $ret ($q->dequeue_nb(3)) { # must stringify in case numeric value $f->set($ret->[0], ''.$ret->[1]); } next unless (defined $event); last if $event eq "ESC"; } $_->exit()->join() for threads->list();

Regards, Mario


In reply to Re: STFL Terminal UI - Concurrency Demonstrations by marioroy
in thread STFL Terminal UI - Concurrency Demonstrations by marioroy

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.