Shared variables along threads

J

Jan Engelhardt

Hi,


as in threads::shared(3pm) I read that one must use
&share([]);
to make a new array available to all threads. How well does this
work with deeper arrays, i.e. does creating a new array using []
within an already-shared array make that one also shared?



Jan Engelhardt
--
 
B

Ben Morrow

Jan Engelhardt said:
as in threads::shared(3pm) I read that one must use
&share([]);

No, one must use
share([]);
or
share [];

Don't use the & unless you know what it does and why you need it to.
to make a new array available to all threads. How well does this
work with deeper arrays, i.e. does creating a new array using []
within an already-shared array make that one also shared?

What happened when you tried it?

Ben
 
J

Jan Engelhardt

as in threads::shared(3pm) I read that one must use
&share([]);

No, one must use
share([]);
or
share [];

Type of arg 1 to threads::shared::share must be one of [$@%] (not single ref
constructor) at tst.pl line 6, near "];"
Execution of tst.pl aborted due to compilation errors.

6: $mx = share [];
Don't use the & unless you know what it does and why you need it to.

& executes a user-defined sub, or for that matter, any non-CORE functions such
as print, etc. Since threads is imported as a module, I assume share is a sub,
and not a CORE function. (p. 78 of 'Introduction to Perl', the Llama book)
to make a new array available to all threads. How well does this
work with deeper arrays, i.e. does creating a new array using []
within an already-shared array make that one also shared?

What happened when you tried it?

See above.

Jan Engelhardt
--
 
B

Ben Morrow

Jan Engelhardt said:
as in threads::shared(3pm) I read that one must use
&share([]);

No, one must use
share([]);
or
share [];

Type of arg 1 to threads::shared::share must be one of [$@%] (not
single ref constructor) at tst.pl line 6, near "];" Execution of
tst.pl aborted due to compilation errors.

This is because you can only share a *variable*, not an anonymous
array. The correct solution is not to simply disable the error by
using &: it is there for a reason, and you need to fix your code,
eg. by assigning it to a variable:

my $x = [];
share $x;
& executes a user-defined sub, or for that matter, any non-CORE
functions such as print, etc. Since threads is imported as a module,
I assume share is a sub, and not a CORE function. (p. 78 of
'Introduction to Perl', the Llama book)

Throw that book away and get another. It is *far* too out-of-date to
be any use. Read perldoc perlsub.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top