![]() |
|
Perl Monk, Perl Meditation | |
PerlMonks |
Tk and Threads (again)by Ace128 (Hermit) |
on Jan 22, 2006 at 14:11 UTC ( [id://524787]=perlquestion: print w/replies, xml ) | Need Help?? |
Ace128 has asked for the wisdom of the Perl Monks concerning the following question:
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: @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: 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: Ideas? Comments? Suggestions? Thanks! Ace
Back to
Seekers of Perl Wisdom
|
|