Tk, is not threadsafe, but you can still use threads with it. Your problem comes from how you are calling the sub Rundibbler. If you call it from a Tk callback, like a Button, timer, etc, it will cause that error. The reason is that when threads get created they get a copy of the parent, at the time of creation. If you are calling it from a Tk callback, or after any Tk statements are in your script, some of the Tk code gets into the thread. Since Tk is NOT threadsafe, everything gets confused, as the Tk code in the parent and thread interfere with each other.
You can use the Super Search here and search for "Tk threads" and find the same advice given.
Basically you need to create your threads BEFORE any Tk code is invoked. That usually means you create the threads and have them in a wait loop for a command(thru shared variables) to start running.Also you cannot try to access a Tk widget from a thread( as in your $reply->insert()), you must juggle shared variables in the main thread, and let the main thread adjust the widgets.
You can try , as a test, to just print to stdout, instead of your thread's $reply->insert();
and see how long it will run. It may well run for awhile, then unexpectedly crash, due to the first reason I mentioned above. BUT if it runs fine, you can share filehandles between threads.
#in main Tk thread
my $fileno = $shash{'fileno'}; #from a shared variable set in thread
print "fileno_m $fileno\n";
open (FH, "<&=$fileno") or warn "$!\n";
# filevent works but may not work on win32
$mw->fileevent(\*FH, 'readable', [\&fill_text_widget,$text]);
#in thread code block
my $pid = open(FH, "top -b |" ) or warn "$!\n";
my $fileno = fileno(FH);
$shash{'fileno'} = $fileno;
To expand your horizons a bit, Perl/Gtk2 has some thread-safety built in, and is easier to use with threads; but it is not perfect, and you can get similar errors with Gtk2.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.