473,608 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=def ine 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_BL OCK:
{
# 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 2184

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

Similar topics

1
4100
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 done after blessing) between all of the mod_perl2 threads. The structure can grow quite big - tenths of thousands of array elements. It can grow as system operates (not possible to construct at apache startup). Sharing array is OK, but inserting...
0
2001
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 distributions using the Native Posix Threading Layer (NPTL), this isn't entirely true anymore and is AFAIK unsupported (using a pid to signal/identify threads). I removed it from my config.sh script and ran Configure -der. make test runs just fine. ...
5
2128
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 user requests the page, the app first tries to retrieve a feed from cache, if the feed has expired, it goes off and request the file from the web. If create a CacheItemRemovedCallback for each item to automatically re-request an expired...
4
2041
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 changing because they are not thread-safe)." Can someone please elaborate on the pros and cons of using static methods? (In VB they are called Shared methods.) The MS DAAB uses Shared methods. This means that all Data Access calls go through...
1
1023
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 on, is when it is needed. For example... Public Class MyClass Dim i as Integer
3
5861
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 mean (especially in relation to each other). Since these two settings seem crucial for performance can somebody explain to me the relationship/difference between these two settings and how they deal with shared memory. Thanks much Sally ...
3
11576
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 days, but am having no luck. I am attempting to write a threaded server program that listens on a socket for requests, then passes the socket's filehandle to an event processor routine, while the listener thread keeps on listening. However, I...
8
1312
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 commands: >>> a=,] >>> b=a >>> b= >>> a
18
2560
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 object, or do I have to write my own explicit destructor? If not, does the default destructor work for pointers (assuming they're non-NULL)?
0
8071
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8503
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8488
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3977
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1345
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.