Put it on the queue? As data to create a new serialport object, not the serialport object itself?

So setup

sub Main { my $qin = Thread::Queue->new(); my $qout = Thread::Queue->new(); my $guithread = threads->create( \&tkgui, $qin, $qout ); ## don't wait for background downloading service / mechtitles threads->create( \&mechtitles, $qin, $qout ); $guithread->join; ## wait for gui to finish return; } ## end sub Main

Then from tkgui somewhere (a button), you tell "mechtitles" to create a newserialport with a message

my $message = { newserialport => [qw/ COM6 baudrate 11 ... /] } ; $qin->push( $message );

Then back in mechtitles, you have a dispatch table, and you use the message from $qin to invoke a callback to create a new serialport (and replace existing serialport)

sub mechtitles { my( $qin, $qout ) = @_; threads->detach(); ## can't join me :) my %dispatch = ( newserialport => \&newserialport, pollserialport => \&pollserialport, ); my %stash; ## STATE require Time::HiRes; while( 1 ) { #~ if( defined( my $url = $qin->popnow ) ) { if( defined( my $action = $qin->pop ) ) { my( $callbackname, $callbackargs ) = %$action; if( my $callback = $dispatch{$callbackname} ){ $callback->(\%stash, $qout, @$callbackargs ); } } Time::HiRes::usleep( 33 * 1000 ); ## microseconds versus milis +econds? grrdoh } } ## end sub mechtitles sub newserialport { my( $stash, $qout, @args ) = @_; my $serialport = Win32::SerialPort->new( ... @args ... ); ... ### THERE IS A NEW STASH IN TOWN $stash->{serialport} = $serialport; } ## end sub newserialport sub pollserialport { my( $stash, $qout, @args ) = @_; ... my $data = $stash->{serialport}->read(1); ## %stash!!!! ... $qout->push( { frobnicatetextdisplay => [ ... $data ... ] ); }

In reply to Re^4: Perl/Tk vs Win32::SerialPort (stash, dispatch, queue) by Anonymous Monk
in thread Perl/Tk vs Win32::SerialPort by Shaoboy

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.