473,372 Members | 1,040 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,372 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 2225
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...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...

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.