473,386 Members | 1,606 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,386 software developers and data experts.

Non-local reference count pointer

ank
Hi, all.

I've come to think of the idea of
automatic initialization/deinitialization of
non-local reference count pointer.

I've made an assumption that the user of the pointer
only read pointer after acquire the reference (increment the ref count)
and when finished using it, the user will release the ref count.

Suppose that we have atomic operations such as:
1. atomic_t atomic_inc(pVal)
2. atomic_t atomic_dec(pVal)
3. bool atomic_cas(pVal, oldValue, newValue)
4. atomic_t atomic_exchange(pVal, newValue)

Then, the pseudocode for initialization/deinitialization
of the non-local pointer to object would be like this.
In addition, no other functions will ever write to
the value of the pointer and reference count.

// initialization of non-local shared pointer
void acquireSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
//assert(sh_count >= 0);
for (atomic_t count;;) {
count = sh_count;
if (count 0) {
if (atomic_cas(&sh_count, count, count + 1))
return;
continue;
}

if (!sh_objPtr) {
Object* obj = createObject();
if (atomic_cas(&sh_objPtr, 0, obj)) {
atomic_inc(&sh_count);
return;
}
destroyObject(obj);
}
suspend_thread(...);
}
}

// deinitialization of non-local shared pointer
void releaseSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
if (atomic_dec(&sh_count) == 0) {
Object* obj = atomic_exchange(&sh_objPtr, 0);
if (obj) destroyObject(obj);
}
}

I have a very little experience to prove that the algorithm
is safe in multithreaded environment but I have been analyzing it
thoroughly.

Is this code safe in multithreaded environment or not?
(I mean for both correctness and safety)
If not, what could be done to improve it?

If anyone has done the same attempt like me but able to prove
correctness and safety, I would like a good suggestion on this topic.
(In fact, I believe that there are many people who had thought about
this
idea before me.)

Thanks for every comments, suggestions in advance.

Sep 20 '06 #1
1 2746
ank wrote:
Hi, all.

I've come to think of the idea of
automatic initialization/deinitialization of
non-local reference count pointer.

I've made an assumption that the user of the pointer
only read pointer after acquire the reference (increment the ref count)
and when finished using it, the user will release the ref count.
This not on-topic for this news group. Try comp.programming.threads.

In general, your code has many problems.

a) multithreaded initialization of an object is very rare in practice
b) You probably want this encapsulated in a class

Look at boost:shared_ptr, it is fairly mature and does somthing very
close to what you seem to want to do.

I prefer to use an intrusive reference count (the reference count is
part of the object being managed).

The most important part of any code design is to see how it is used, try
using your "acquireSharedObject" in some test code, I think you'll see
it's not very easy to use.

>
Suppose that we have atomic operations such as:
1. atomic_t atomic_inc(pVal)
2. atomic_t atomic_dec(pVal)
3. bool atomic_cas(pVal, oldValue, newValue)
4. atomic_t atomic_exchange(pVal, newValue)

Then, the pseudocode for initialization/deinitialization
of the non-local pointer to object would be like this.
In addition, no other functions will ever write to
the value of the pointer and reference count.

// initialization of non-local shared pointer
void acquireSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
//assert(sh_count >= 0);
for (atomic_t count;;) {
count = sh_count;
if (count 0) {
if (atomic_cas(&sh_count, count, count + 1))
return;
continue;
}

if (!sh_objPtr) {
Object* obj = createObject();
if (atomic_cas(&sh_objPtr, 0, obj)) {
atomic_inc(&sh_count);
return;
}
destroyObject(obj);
}
suspend_thread(...);
}
}

// deinitialization of non-local shared pointer
void releaseSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
if (atomic_dec(&sh_count) == 0) {
Object* obj = atomic_exchange(&sh_objPtr, 0);
if (obj) destroyObject(obj);
}
}

I have a very little experience to prove that the algorithm
is safe in multithreaded environment but I have been analyzing it
thoroughly.

Is this code safe in multithreaded environment or not?
(I mean for both correctness and safety)
If not, what could be done to improve it?

If anyone has done the same attempt like me but able to prove
correctness and safety, I would like a good suggestion on this topic.
(In fact, I believe that there are many people who had thought about
this
idea before me.)

Thanks for every comments, suggestions in advance.
Sep 20 '06 #2

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
2
by: Mark Stijnman | last post by:
I would like to be able to have an object accessible as a vector using the operator, but able to track modifications to its data, so that it can update other internal data as needed. I figured...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
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...

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.