Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^3: Weird Output with Threads and NCurses

by var121 (Novice)
on Jul 19, 2016 at 14:24 UTC ( #1168059=note: print w/replies, xml ) Need Help??

in reply to Re^2: Weird Output with Threads and NCurses
in thread Weird Output with Threads and NCurses

Thanks for your post, I will go through the concept of Asynchronous programming. What is the last argument in  addRepeatCallback call ? Does First argument define the time ? and we are assuming the first call will be completed in 0.010 time units ? For writing games, where we will have to do many things simultaneously, like keeping score, updating screen, is that possible with Async Technique ? I have this doubt because I have seen some simple game code, written using threads, and it works absolutely fine.

Replies are listed 'Best First'.
Re^4: Weird Output with Threads and NCurses
by Anonymous Monk on Jul 19, 2016 at 15:55 UTC

    The calls are documented in POD at the end of Async::Tiny. Just run "perldoc Async/" to see the formatted version.

    The first argument to addRepeatCallback is the interval in seconds, and the second is the callback subref. Any extra arguments are provided to the callback sub when it is called.

    In this case they are the row number at which to display the count, and an array ref which holds as its first element the actual count. This array ref holds the "state" for this particular "thread" of repeated callbacks, so I can have two (or more, if desired) independent counts running at the "same" time using the same counter sub.

    ( see the "counter" sub for how those two extra arguments are handled.)

    "simultaneously" ? yes and no :)
    My example program shows two counters incrementing "simultaneously" at two different rates.

    Here's another example program (a newer version of the "" in the POD) where each row sweeps at its own rate independent of the other rows (and "simultaneously").

    (This is one of my test programs for the timerqueue.)

    #!/usr/bin/perl # # - multiple independent timers using Async::Tiny Ppoll use Curses; use Term::ReadKey; use Async::Tiny; use strict; use warnings; my ($width, $height) = GetTerminalSize; my @lines = ( '-' x $width ) x $height; my $endcode; my $t = Async::Tiny->new; $t->addDelayCallback( 100, sub { $endcode = 'endrepeat' } ); for my $row (0 .. $#lines) { $t->addRepeatCallback( (3 + rand 20) / 50 , sub { s/-/#/ or s/#*\K#/=/ or tr/=/-/ for $lines[$row]; addstr $row, 0, $lines[$row]; refresh; $endcode; } ); } initscr; curs_set 0; addstr 0, 0, '-' x $width x $height; $t->eventloop; endwin;

    I'll see if I can do a simple game with action, interaction, and score keeping as "proof of concept" sometime soon.

      Simple game? (note: I've never played the original or any variation thereof).

      It's just a proof of concept :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1168059]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-03-17 06:43 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (223 votes). Check out past polls.