By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Non-local reference count pointer

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.