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


In reply to Tk and thread local storage 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.