Sorry, should have submitted example code, I wasn't being clear: I'm passing objects to the thread through shared variables after it's been created. This code demonstrates what I mean - note that it won't complete successfully as long as the *STDIN line remains uncommented. But I'm looking for the best way of making something like that work!

#!/usr/bin/perl -w use strict; use threads; use threads::shared; use IO::Handle; use Thread::Semaphore; # Here we've a choice - use Storable to freeze and thaw stuff, or # these two bogus implementations which do nothing. This illustrates # (when you use the bogus code) how you cannot simply drop blessed # objects into shared objects. If you use Storable, blessed objects # can be passed across while they're frozen, but file handles (for # example) still can't. use Storable qw{freeze thaw}; #sub freeze { ${$_[0]} } #sub thaw { \shift } # Job queue and semaphore; the semaphore gets signalled every time an # object is pushed onto the queue. my @queue : shared; my $queue_ctrl = Thread::Semaphore->new(0); sub thread { while(1) { $queue_ctrl->down; # Wait for something to be queued lock @queue; last unless @queue; # Terminate on empty packet my $object = ${ thaw shift @queue }; # Would normally receive something relatively complex and do some # work with it here. print "Thread received $object.\n"; } } # Note that I'm creating the thread *first* and *afterwards* passing # objects across (through the queue) for it to work with.. my $thr = threads->new(\&thread); # Things we want to try passing through to the thread: my @stuff = ( # A basic type, works no problem with or without freeze/thaw '"hi there"', # A blessed reference, must be frozen and thawed bless({}, 'Foo'), # We can pass STDIN's file descriptor as an integer STDIN->fileno, # Have to comment this next line out for successful completion - but # this (or some equivalent) is what I would like to achieve! *STDIN, ); # Queue stuff and see what happens foreach my $obj ( @stuff ) { { # Queue $obj and signal $queue_ctrl lock @queue; print "\nEnqueueing $obj...\n"; push @queue, freeze \$obj; $queue_ctrl->up; } sleep 1; # Optional, but gives the thread a chance to grab the queue } # Terminate - signal $queue_ctrl without enqueueing anything. $queue_ctrl->up; $thr->join;

In reply to Re^2: Passing globs between threads by conrad
in thread Passing globs between threads by conrad

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.