Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Tk and thread local storage

by nikosv (Hermit)
on Nov 15, 2010 at 06:30 UTC ( #871407=perlquestion: print w/ replies, xml ) Need Help??
nikosv has asked for the wisdom of the Perl Monks concerning the following question:

More like playing around with the TLS model rather than any practical value, I wanted to look into what happens with threads and tk in more detailed way.

So I have a window in the main thread with a buttons and when the button is clicked I fire a thread from within the callback. Of course ill advised since tk is not thread safe but done for demonstration purposes only.

use Tk; use threads; $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 { $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;

What happens is that the $mw and the window structure get duplicated (but there is no window to be seen) in the worker thread but they both share the same window handle:

print "from Main thread : ", $mw, ":::",$mw->id,"\n\n"; from Main thread : MainWindow=HASH(0x1c57f64):::0x002E0528 print "from Worker thread ", $mw, ":::",$mw->id,"\n\n"; from Worker thread : MainWindow=HASH(0x1e0a98c):::0x002E0528

There two distinct HASH references sharing the same window handle ($mw->id) but when I manipulate the worker thread window like changing its title $mw->title("Worker thread window") the title of the Main thread window is changed since they reference the same window handle. I am specullating that since the handle is a kernel object it does not get copied.

In fact if you continue clicking the button you will see getting more copies of the window but always with the same handle. Checked with Data:Dumper($mw) too and it is consistent that we get a totally distinct structure each time. So when using threads from within TK we get distinct copies of the window struct but all sharing the same handle

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

Comment on Tk and thread local storage
Select or Download Code
Re: Tk and thread local storage
by BrowserUk (Pope) on Nov 15, 2010 at 07:51 UTC
    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://871407]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-09-02 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls