Threading - share

M

mumebuhi

I am trying to understand the differences among these constructs:

# 1
my $p : shared;
$p->{'_name'} = "My Name";

# 2
my $p : shared = &share({});
$p->{'_name'} = "My Name";

Do 1 and 2 achieve the same thing?

# 3
my %p : shared;
$p{'_name'} = "My Name";

Is it fair to say that 3 is the simpler form compared to 1 and 2?

Thank you.

Buhi
 
X

xhoster

mumebuhi said:
I am trying to understand the differences among these constructs:

# 1
my $p : shared;
$p->{'_name'} = "My Name";

# 2
my $p : shared = &share({});
$p->{'_name'} = "My Name";

Do 1 and 2 achieve the same thing?

No. 1 gives a "Invalid value for shared scalar" error in nontrivial
contexts. 2 doesn't (but overriding prototypes is probably bad form,
anyway).

# 2.5
my $p = share(%{+{}});
$p->{'_name'} = "My Name";

# 2.75
my $p={}; share %$p;
$p->{'_name'} = "My Name";
# 3
my %p : shared;
$p{'_name'} = "My Name";

Is it fair to say that 3 is the simpler form compared to 1 and 2?

In the sense that it is simpler to not use gratuitous references than it is
to use gratuitous references, sure, 3 is simpler. But using the same
logic, we could say it is even simpler not to share the variables at all,
as you don't make meaningful use of the shared nature. Whether you need
the complexity of sharing, or need the complexity of references, depends on
the parts of your code you haven't shown us.

Xho
 
M

mumebuhi

mumebuhi said:
No. 1 gives a "Invalid value for shared scalar" error in nontrivial
contexts. 2 doesn't (but overriding prototypes is probably bad form,
anyway).

# 2.5
my $p = share(%{+{}});
$p->{'_name'} = "My Name";

# 2.75
my $p={}; share %$p;
$p->{'_name'} = "My Name";


In the sense that it is simpler to not use gratuitous references than it is
to use gratuitous references, sure, 3 is simpler. But using the same
logic, we could say it is even simpler not to share the variables at all,
as you don't make meaningful use of the shared nature. Whether you need
the complexity of sharing, or need the complexity of references, depends on
the parts of your code you haven't shown us.

I tend to use #3 for a regular usage. However, when I need to construct
a new object, that will be shared by several threads, I have to use #2
in the constructor. Is this reasonable?
 
X

xhoster

mumebuhi said:
I tend to use #3 for a regular usage. However, when I need to construct
a new object, that will be shared by several threads, I have to use #2
in the constructor. Is this reasonable?

Yes. I happen to use 2.75 because of a (perhaps irrational) aversion to
using &. But 2 is fine, and even recommended by the docs. Of course,
if whatever module you are using is not thread safe, then may have
problems (for example with DESTROY being called multiple times), but that
is true regardless of the exact syntax you using here.

Xho
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top