in reply to Tk and thread local storage

I am asking for some aid in verifying the validity of this test and if someone could shed more light into the internals/what is actually happenning

I can confirm what you are seeing, and how you are interpreting it is correct.

When you start your worker thread, the perl variable $mw, which is closed over by the sub worker_thread() is cloned. But, as the tk internals are separate and unknown to Perl, it does not clone anything in tk space. Hence the two copies of $mw refer to the same tk state that hides behind it.

The problem is, that tk is not expecting to be called concurrently from multiple threads, so if you start making any kind of mutating use of those two copies, then tk's unprotected internal state will get corrupted and your program will crash.

If you were using strict & warnings you would see:

c:\test>junk.pl8 Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 6. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 7. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 10. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 11. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 15. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 15. Global symbol "$t" requires explicit package name at C:\test\junk.pl8 +line 18. Global symbol "$t" requires explicit package name at C:\test\junk.pl8 +line 19. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 23. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 24. Global symbol "$mw" requires explicit package name at C:\test\junk.pl8 + line 24. Execution of C:\test\junk.pl8 aborted due to compilation errors.

And if you cleaned those up you'd get:

c:\test>junk.pl8 from Main thread MainWindow=HASH(0x3a2e2c8):::0x00C90356 Attempt to free non-existent shared string '_TK_RESULT_', Perl interpr +eter: 0x3ac8314 at C:/perl32/lib/Tk/Submethods.pm line 37. from Worker thread MainWindow=HASH(0x3c926a4):::0x00C90356 Free to wrong pool 3ac5dc8 not 34ae9c8 at C:/perl32/lib/Tk/Widget.pm l +ine 98 during global destruction. Font {MS Sans Serif} 8 still in cache.

The "solution" is do not access tk from multiple threads; it will always crash. Usually sooner rather than later.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Tk and thread local storage
by nikosv (Deacon) on Nov 15, 2010 at 17:29 UTC

    BrowserUk,well-set explanation,as always!

    And if you cleaned those up you'd get: done that but removed it for clarity since falls into the thread safety-breaking category

    cheers

      but removed it for clarity

      I'm not sure how the extra two lines at the top and two additional mys detract from clarity, but each to their own :)

      [16:18:28.03] C:\test>type junk.pl8 #! perl -slw use strict; use Tk; use threads; my $mw = new MainWindow; $mw->minsize(qw(300 50)); $mw->title("Main thread window"); my $button = $mw -> Button(-text => "Fire Thread", -command => \&fire_thread) -> pack(); print "from Main thread ", $mw, ":::",$mw->id,"\n\n"; sub fire_thread { my $t = threads->create(\&worker_thread); $t->detach(); } sub worker_thread { $mw->title("Worker thread window"); print "from Worker thread ", $mw, ":::",$mw->id,"\n\n"; } MainLoop;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        because the use of my in
        my $t = threads->create(\&worker_thread);
        would kill the execution immediatelly and I wanted to make it continue