473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Question about threads::shared and blessed references

I am trying to use threads::shared to make a thread-safe object.

It appears to be the case that copying a blessed-then-shared reference
to another shared variable keeps the underlying structure intact
(e.g., hashref & its contents) but forgets the blessedness. This makes
my program pretty useless because I need to be able to copy or pass my
lockable object(s) at various times in my program, without losing
their identity as blessed objects.

The attached program demonstrates what I am trying to say. It's output
is:

ref $f = LockableObject
ref $copy = HASH

Note that there are no threads involved here; this is all executing in
a single thread.

Also note that if you remove the share() call from
LockableObject::new(), make $copy a non-shared variable, and comment
out the *LOCK_BLOCK blocks - in other words, if you remove just the
threadedness of the program - then the program works "right". In this
case the output is what I had originally expected, to wit:

ref $f = LockableObject
ref $copy = LockableObject

What am I missing?

(P.S. perl -V says "Summary of my perl5 (revision 5.0 version 8
subversion 0)...usethreads=define use5005threads=undef
useithreads=define usemultiplicity=define" and a bunch of other things
that are available upon request if they are relevant.)
---------------------------------------
#!/usr/bin/perl -w

use strict;

package LockableObject;

use threads;
use threads::shared;

sub new {
my $obj = bless { }, shift;
# Imagine that $obj has some internals that I want to protect,
# so I need to be able to lock the object to serialize access
share($obj);
}

package main;

use threads;
use threads::shared;

my $f = new LockableObject;

print "ref \$f = ", ref $f, "\n";

LOCK_BLOCK:
{
# The object is lockable. Look, no errors!
lock ($f);
}

my $copy : shared = $f;

ANOTHER_LOCK_BLOCK:
{
# The copy of the object is still lockable!
lock ($copy);
}

# But the copy is no longer a LockableObject reference
print "ref \$copy = ", ref $copy, "\n";

1;
Jul 19 '05 #1
0 2166

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dennis Gavrilov | last post by:
Hi, All! I have two questions: strategic and technical. Technical one first: I need to share an array of objects (implemented as hashes, having references to other objects and hashes, sharing...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
5
by: Richard P | last post by:
I need some help on timers. My app is asp.net 1.1 website running in a shared hosting environment with a third-party service provider. I currently request and cache 20 - 40 remote RSS feeds. When a...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
1
by: Joel Denton | last post by:
I have a question about when synclock is necessary. Most of the examples I've seen have suggested synclocking on a static/shared object for synchronizing access to class data. What I'm not clear...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
3
by: Sako | last post by:
I am trying to teach myself perl by writing a program I've been meaning to implement, so I am pretty green in perl. I'm having problems sharing filehandles opened by a thread - been RTFM for a few...
8
by: zefciu | last post by:
Hello! Where can I find a good explanation when does an interpreter copy the value, and when does it create the reference. I thought I understand it, but I have just typed in following...
18
by: Ulrich Hobelmann | last post by:
Assuming I have an object containing a reference to something (which is passed along with the object's constructor), does the automatic default destructor take care of destroying the reference's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.