Well, hello there!
Let's see now... I have this main script starting up threads and then Tk (yes, done alot of research on the matter. Need to start threads before Tk). For each thread there is a Thread::Queue::Any. I have a plugin idiom.. Well, code for starting up threads:
foreach (keys %plugins) {
my $thread_queue = Thread::Queue::Any->new;
my @thread_subs;
$plugins{$_}->SUBS_AS_THREADS(\@thread_subs, $thread_queue);
foreach my $sub (@thread_subs) {
async { $sub->($plugins{$_}, \%plugins, $thread_queue, $mw) };
}
}
...
...
MainLoop;
@thread_subs is an array of subrefs containing all subs(refs) a surtain plugin wanna start up as a new thread.
$plugins{$_} is the current plugin (I loop through them all here.)
Below is an example of a threadsub:
sub _thread_sub_in_a_plugin {
my $self = shift;
my $plugins_ref = shift;
my $thread_queue = shift;
my $mw = shift;
#$self->{'thread_queue'} = $thread_queue;
$$plugins_ref{'imagePlugin'}->{'thread_queue'} = $thread_queue;
# The 2 above same thing, really...
while (1) {
my ($full_path) = $thread_queue->dequeue;
...
}
}
As you can see, I found this solution quite nice, since I use Tk, and wanna use threads. Since I dunno how many threads a plugin wanna start (and more importantly, IF a plugin wanna start a thread), I have a sub that sets refrences into an array to subs a surtain thread wanna have as threads (@thread_subs).
Now, notice the $mw above. The idea was that
if a plugin wanted to do some nice GUI stuff (since this is the main point having it as a seperate thread, so it all goes smoothly), the $mw was passed in so it could do something like $mw->toplevel, and use that to do stuff on. However, everything that is created within this thread and has to do with Tk, makes me get the error "Free to wrong pool 2741b8 not 64a0778 at ..." thing, when it wanna destruct something created in there (why? I mean, nothing should get destructed, since the thread isnt done (while (1) ) ).
The main reason I do this is, since its an image plugin, I wanted it to generate an image based on what the path is it gets from the $thread_queue.
Now, I had 3 ideas for solutions:
1. If it was possible, let the other thread have it's own Tk Mainloop and everything. No collision with the main one. This seems impossible to do.
2a. Return the data created (by imagequick in this case), and put it in the Toplevel in the main thread. This would mean that the imagecreation process would be in a seperate thread, and then pass back the data through Queue::Any, and put into the Toplevel object. However, this still means that the main thread has to wait until the imagequick thread is done, so it really seems pointless this too, but I would avoid the destruction problem...
2b. I was also thinking that I could, however, have a $mw->repeat(10, ...) set, to update if there was something in the $thread_queue... However, I dont really like that idea. Such a waste of CPU and not "realtime"... I would prefer that something happends when thread is done! Or what do you guys think about it repeating every 10 ms? Checking if there is something new in the $thread_queue.
3. I was also looking into letting the imagethread create a GUI using wxPerl instead! This could work (although I havent manage to make it work yet...) But, I would prefer not to use it, since I already have Tk... :) Anyone here managed to use wx and tk at the same time? (In two seperate threads then I suppose, since both have a ->Mainloop call, that waits for the GUI stuff to end...)
Hope I didnt miss anything. Im so into this, its all very clear to me what the problem is. Explaining it to others, avoiding telling a shortstory, is tricky ;)
The idea:
Main(Tk) imagePlugin
|
|
|------------*
| |
| generate and show image
|
|
Ideas? Comments? Suggestions?
Thanks!
Ace
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.