If you look closely at my thread's code block, you will see it is wrapped in a while loop.....I call it a sleeping thread, and you control it thru shared variables. When you create the thread, it keeps looping(and sleeping a bit) until it gets a go variable change. Here is a simple example of a sleeping thread. As you can see from my other complex example, you need to keep track of everything in hashes if you have more than one threads. It also helps to have an event-loop system, so you don't need difficult while() loops in main to watch the threads, but a clever while loop will work. SeeRoll your own Event-loop and Readkey with timer using Glib

Don't forget, you can modify that thread's while loop to suit your purposes, you just need to be creative to suit your exact situation. Also remember, you can share filhandles across threads thru the fileno. So you might want to communicate by print to a filehandle in the thread, and reading it in main.

#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; my $data:shared = ''; my $go_control:shared = 0; my $die_control:shared = 0; my $thr = threads->new(\&excecute); print "press a key to start thread\n"; <>; $go_control = 1; for( 1.. 10){ print "\t\t$data\n"; select(undef,undef,undef,.25); } print "press a key to stop thread\n"; <>; $go_control = 0; print "press a key to kill thread\n"; <>; $die_control = 1; print "press a key to exit\n"; <>; sub excecute{ my $count; while(1){ if($die_control){ print "thread finishing\n"; return} #wait for $go_control if($go_control){ if($die_control){ print "thread finishing\n"; return} $data = $count++; print $data,"\n"; select(undef,undef,undef,.25); }else{ select(undef,undef,undef,.25); }# sleep until awakened } return; }

I'm not really a human, but I play one on earth CandyGram for Mongo

In reply to Re^4: Threads memory consumption is infinite by zentara
in thread Threads memory consumption is infinite by Godsrock37

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.