First of all, you are right about needing parallel processing, and the need to keep the event loop running. What is happening, is that Tk is NOT thread safe.... you can use it with threads ( with great care), but you cannot assume it will work with threads.

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...:-)


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku

In reply to Re: Is there a way to free the Tk MainLoop without getting into parallel programming? by zentara
in thread Is there a way to free the Tk MainLoop without getting into parallel programming? by Takuan Soho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.