.... i just hack at these things... but the first thing i would try is avoid the intermediary $twit, directly put it into the shared hash
$thread_hash{'twit'} = Net::Twitter->new( username => $user, password => $pass );
.... another trick is to confine all your Twitter stuff to the thread.... is there really a need to keep a global twitter object?..... just put all the twitter stuff into the thread....
....with a sleeping thread model, as in Reusable threads demo .... and here is a Tk example with threads....notice the thread is created before any GUI code is invoked...... this goes along way toward making threaded GUI's work
#!/usr/bin/perl use strict; use Tk; use threads; use threads::shared; my $data_out:shared = ''; my $data_in:shared = ''; my $thread_die = 0; my $wthr = threads->new( \&update_thread )->detach; #create Tk stuff my $mw = MainWindow->new( -background => 'black', -foreground => 'yellow', -title => "Thread Test" ); $mw->geometry("600x400"); $mw->minsize( 400, 400 ); $mw->maxsize( 800, 800 ); my $sent_text = $mw->Scrolled('Text', -height => 10, -width => 60, -background => 'black', -foreground => 'yellow' )->pack( -side => 'bottom', -anchor => 's', -pady => 2 ); my $received_text = $mw->Scrolled( "Text", -height => 10, -width => 60, -background => 'white', -foreground => 'black', )->pack(); my $repeater = $mw->repeat(1000, sub{ $data_out++; $sent_text->insert( 'end', "Sent $data_out\n" ); $sent_text->see('end'); $received_text->insert( 'end', "Received $data_in\n" ); $received_text->see('end'); }); $mw->Button(-text=> 'Exit', -command => sub{ $thread_die = 1; $repeater->cancel; $mw->withdraw; kill 9, $$; })->pack; MainLoop; ######################################################### #the non-Tk worker code sub update_thread { print "update_thread called...\n"; while (1) { if($thread_die == 1){return} $data_in = 'thread-processing'.$data_out; sleep 1; } }
GUI's are notorious for having problems with threads..... another no-no with gui's is you can't manipulate the GUI (access widgets) in the main thread from the spawned thread..... Gtk2 has made a great improvement in this aspect, by the GLib::Idle_add method, which you can call from a spawned thread..... google for "glib idle_add" if interested..... Wx may have the idle_add method, since many systems use Gtk2 as the base for Wx.... but not always.
In reply to Re: passing shared blessed object part 2
by zentara
in thread passing shared blessed object part 2
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |