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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tk and thread local storage
by BrowserUk (Patriarch) on Nov 15, 2010 at 07:51 UTC | |
by nikosv (Deacon) on Nov 15, 2010 at 17:29 UTC | |
by BrowserUk (Patriarch) on Nov 15, 2010 at 17:39 UTC | |
by nikosv (Deacon) on Nov 15, 2010 at 18:00 UTC |