Dear monks,

I just installed Activestate Perl 5.8 Beta Built 802 under Win2k and played around a bit with threads. Due to the good tutorial at YAPC::Europe everything worked fine...

Then I got a bad idea; i remembered an old DOS program which waited for an input for n seconds, and if it didn't come within those n seconds, it continued with a standard value. I forgot its name, but maybe you know it. Well, might be possible doing so by using threads, I thought...

Here are some lines of code:

#! /usr/bin/perl use 5.008; use strict; use warnings; use threads; use threads::shared; our $Input : shared = ""; sub getInput { print "Please enter some- or nothing: "; my $input = <STDIN>; chomp($input); $Input = $input if defined $input; # or whatever... } my $oldInput = $Input; my $inputThread = threads->create('getInput'); sleep 5; unless ($Input eq $oldInput) { $inputThread->join(); print "Input was: $Input\n"; sleep 2; } else { print "Continuing with 'abcd' as a standard value...\n"; # WHAT CAN I DO HERE ? }
Well, my problem is how to get rid off the blocked thread if the user doesn't enter anything... If I run $inputThread->join, the program will block forever. If I run $inputThread->detach() (or do nothing), I will at least be able to continue with my main program, but have got a blocked thread somewhere, and if a user enters anything later, it might give strange results because the shared Variable $Input suddenly has got another value.

Do you know how to solve this problem? (It is not a production problem, but I'm very interested if and how this can be done with threads; well, with fork it wouldn't be a big problem...)

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"


In reply to "killing" perl5.8-threads by strat

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.