in reply to Is there a way to free the Tk MainLoop without getting into parallel programming?
What happens, is that when a thread gets spawned, it gets an exact copy of the parent thread, at the time of spawning. So you get some Tk code fragments, from the parent thread's mainloop, into the thread, and since Tk is NOT threadsafe, it raises havoc with the event system, as both threads try to run the event loop.
The general rule, is to create your threads, before any Tk code is invoked. This is done by spawning your thread before you create the $mw in the parent thread. I usually use a sleeping thread thread model, where I can create numerous threads, which are help in a sleep loop until given a signal to go. After the sleeping threads are created, go ahead and make your Tk control GUI, and use shared variables to signal the child threads to start/stop/return etc.
See PerlTk on a thread... and Tk-with-worker-threads for example..... this has been discussed many times before, and googling for "perl tk threads" will yield many code examples.
BTW, Gtk2 has better built-in thread safety, if you want to move up to it.
P.S. There is a module I saw recently( I didn't test it), that supposedly will only copy relevant code, when creating threads avoiding this copy-on-create problem.... I don't know if it works with Tk, or even at all, but its worth a search for...... me...I'm on vacation...:-)
|
|---|