473,406 Members | 2,710 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,406 software developers and data experts.

Atomic reference counting

I need to implement reference counting in a class hierarch,
in a thread safe manner. (The classes are umanaged but I
might want to compile them with the /clr option.)

Some of the objects - atoms - are registered in a global hash
table - let's call it an atom table. When an atom refernce
count reaches zero, the atom is removed from the table.

The catch is, after an atom has been counted down, another
thread might find it in the atom table and create a reference
to it before it has been removed.
In an assembler version of the program, I did this (hope the
following mix of mumbojumbo and pseudocode is clear):

- The atom table is guarded by a critical section.
- The objects in the table are chained together unidirectionally.
It is possible to have more objects with the same key in the
chains, though only one of can be valid.
- Atoms in the table are valid when their reference count is
not zero.

For this to work, I needed atomic operations for:

1. increment and tell if now one
2. decrement and tell if now zero.

Since the LOCK INC instruction doesn't tell if it reached one,
I decided to offset all refcounts by -1, so I can test the z (zero)
flag after the increment, and the s (sign) flag after decrements
will tell me if I got below 0.

This works for me in ASM but I'd rather trash the asm code
and get to C++.

Any suggestions how???

The intrinsics don't seem to cover doing a locked inc/dec
and testing its flags, or have I ovelooked something - or
is there a better way to accomplish what I want?

Jul 20 '06 #1
3 2227
Hi Ole,
1. increment and tell if now one
2. decrement and tell if now zero.
Since the LOCK INC instruction doesn't tell if it reached one,
I decided to offset all refcounts by -1, so I can test the z (zero)
flag after the increment, and the s (sign) flag after decrements
will tell me if I got below 0.
This works for me in ASM but I'd rather trash the asm code and get to
C++.

Any suggestions how???

The intrinsics don't seem to cover doing a locked inc/dec and testing
its flags, or have I ovelooked something - or is there a better way to
accomplish what I want?
I'm not sure if I'm missing something, but aren't you looking for InterlockedIncrement
and InterlockedDecrement?
http://msdn.com/library/en-us/dllpro...dincrement.asp

--
Best regards,
Kim Gräsman
Jul 20 '06 #2
Hi Ole,

Please take a look at the Interlocked* functions from the WinAPI they
used for atomic operations.
Or maybe, since you're using the CLR extensions, you might be
interessted in the .NET Library class Interlocked

http://msdn.microsoft.com/library/de...classtopic.asp

BR
Vinzenz
Jul 20 '06 #3
"Vinzenz Feenstra" <vi**************@blog.evilissimo.netwrote:
Hi Ole,

Please take a look at the Interlocked* functions from the WinAPI they used
for atomic operations.
Or maybe, since you're using the CLR extensions, you might be interessted
in the .NET Library class Interlocked

http://msdn.microsoft.com/library/de...classtopic.asp
Thanks - this seems to be what I need. I wasn't aware of these
routines or the lock xadd instructiont they use. Seems I've been
clinging to i386 assembler docs for too long...
Jul 20 '06 #4

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

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
8
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example,...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
5
by: hikums | last post by:
Begin Atomic Declare a integer; Set a=(select count(1) from claims.table1); if a<>0 then import into c:\tmptyfile.txt of del replace into claims.table1; end if; End!
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
0
by: Chris Thomasson | last post by:
Here are the pre-alpha code downloads: http://appcore.home.comcast.net/vzoom/refcount/ (both C and C++ api here...) Here is some pre-alpha documentation: ...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
10
by: Dmitriy V'jukov | last post by:
On 16 ÍÁÊ, 21:41, "Dmitriy V'jukov" <dvyu...@gmail.comwrote: According to definition, release operation on 'std::atomic_global_fence_compatibility' can only 'synchronize with' acquire...
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.