I have an application which is command line based and I decided to give it a face-lift by doing a GUI with Perl/Tk (on the Windows platform).

There is a button called 'GO' and its callback (&go_command) calls a sub that creates a new thread. As many nodes suggest, this won't work since Perl/Tk is not thread safe but I did conduct an experiment anyway to see for myself.

sub go_command { . . . my $thr = threads->create(\&sub1); if ($thr->is_joinable()) { $thr->join(); } } sub sub1 { print("In the thread\n"); }

When the thread is created and no join() or detach() is used against it, the application does not crash

However on the opposite case, the app crashes with "Free to wrong pool ....Perl/site/lib/Tk/Widget.pm line 98 during global destruction."

When a Perl/Tk app is exiting normally (e.g. by use of $mainwindow->destroy()) the following process is initiated:
All the widgets go through the following subroutine of Widget.pm:

sub DESTROY { my $w = shift; $w->destroy if ($w->IsWidget); }

which subsequently calls :

sub _Destroyed { my $w = shift; my $a = delete $w->{'_Destroy_'}; if (ref($a)) { while (@$a) { my $ent = pop(@$a); if (ref $ent) {....}

The main window is the only widget that goes through it last and the only widget that goes beyond the if (ref($a)) point .

It seems that when join or detach try to do their clean up, as perlthrtut says :

"join() also performs any OS cleanup necessary for the thread. That cleanup might be important, especially for long-running programs that spawn lots of threads",

they do something that triggers the destruction of the window as well.

Specifically when join() is called the sub DESTROY is called and it tries to go through the normal exiting process for every widget as described above.

However when it reaches point 'if (ref($a))' for the first widget to kill (in this case the button) it crashes with the "Free to wrong pool" message.

Now why would that be? I hypothesized that when the OS cleans up for the newly created thread it cleans up for the main thread as well.

Further investigation makes me think that Perl/Tk is hit by the same issue as Storable 2.09 as documented threads.t and make Storable thread-safe in that Perl Tk uses the same context for different threads and when the OS tries to clean up the memory pool of the newly created thread it cleans up the memory pool of the primary thread as well :

From threads.t about Storable:

as of 2.09 on win32 Storable w/threads dies with "free to wrong pool" since it uses the same context for different threads. since win32 perl implementation allocates a different memory pool for each thread using the a memory pool from one thread to allocate memory for another thread makes win32 perl very unhappy

Do you think that my assumption that both Tk and Storable 2.09 suffer because of the same issue, is correct? and what term would more accurately describe the situation?

1.Perl Tk is not thread safe? (I'm not calling any Tk components from multiple threads)

2.Perl Tk is not thread-friendly? (maybe better since I am calling a thread from within a Tk component?)

3.Or Perl Tk is not re-entrant?


In reply to Perl Tk and threads by nikosv

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.