in reply to Re^2: threads with shared variables across multiple instances of a module
in thread threads with shared variables across multiple instances of a module
Yes, you can share individual elements of a hash (using share()):
use strict; use warnings; use threads; use threads::shared; my %h; share $h{ fred }; $h{fred} = 2; async{ my $a='bill'; lock $h{fred}; $h{fred}=$a; }->join; print $h{fred}; bill
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: threads with shared variables across multiple instances of a module
by zerg13new (Initiate) on Mar 26, 2010 at 07:40 UTC | |
can i share the whole hash ?? a can't push in my shared hash pair like this: "192.168.0.1:3128" => $client $client is a local (defined with "my") variable (socket)
part of my code:
help me please, desparing | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Mar 26, 2010 at 16:19 UTC | |
This *client{IO}; is invalid syntax when $client is a lexical. It would have to be *{ $client }{IO}. But that still won't help you because of an (unnecessary) restriction that doesn't allow you to store globs, (or references to globs) into shared scalars. There are two ways to share globs (filehandles sockets etc.) between threads: I've never understood this restriction. The fact that a glob can be successfully shared via the latter method suggests strongly that the restriction both artifical (they just decided to reject glob(ref) assignments to shared scalars--and the code supports this); and unnecessary. But those that should know the answer to why this restriction was established and persists, have chosen not to answer that question. 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.
| [reply] [d/l] [select] |
|
Re^6: threads with shared variables across multiple instances of a module
by zerg13new (Initiate) on Mar 27, 2010 at 07:52 UTC | |
perhaps i didn't understand, but i have the same error "Thread 1 terminated abnormally: : Bad file descriptor". can explain to me ?
| [reply] [d/l] |
by BrowserUk (Patriarch) on Mar 27, 2010 at 17:01 UTC | |
If you included the full error message (including the line number), I'd have a chance of knowing which thread terminated abnormally. If you included all the source code, I might stand a chance of trying it out for myself. If you posted the code without the line numbers, I wouldn't have to strip them off. And your code would be far easier (for you and me) to read and analyse if it was formatted consistently. As it is, the most obvious problem is that %clients isn't declared (or shared) anywhere. And BTW, this %clients = ( %clients, "$client_ip:$client_port" => fileno($client) ) ; is a really horrible way to add a new key/value pair to a hash. A simple:
is much easier to read and far more efficient. You've also failed to do any locking of your shared hash. (Assuming it is actually declared & shared somewhere in the code you've omitted!) 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.
| [reply] [d/l] [select] |
by zerg13new (Initiate) on Mar 29, 2010 at 07:48 UTC | |
new attempt: This is WHOLE code of my server.pl at now
OUTPUT of my server.pl: it is better ?? sorry for my illiteracy =) | [reply] [d/l] [select] |
by zerg13new (Initiate) on Mar 29, 2010 at 08:00 UTC | |
new attempt: This is WHOLE code of my server.pl at now
OUTPUT of my server.pl: it is better ?? sorry for my illiteracy =) yeap, i have not any locking, cause it will... in future )) | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Mar 29, 2010 at 08:16 UTC | |
by zerg13new (Initiate) on Mar 30, 2010 at 15:57 UTC | |
by zerg13new (Initiate) on Apr 03, 2010 at 14:07 UTC | |