By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 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.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Sep 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
[cross-posting deleted]

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.

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.
You probably want to try on comp.programming.threads or similar since
threading is technically off-topic here (see
http://www.parashift.com/c++-faq-lit....html#faq-5.9).

Cheers! --M

Sep 20 '06 #2

P: n/a
ank
mlimber wrote:
[cross-posting deleted]

You probably want to try on comp.programming.threads or similar since
threading is technically off-topic here (see
http://www.parashift.com/c++-faq-lit....html#faq-5.9).
I'm not sure that the OP is off-topic.
However, if this is off-topic then I would have to apologize here.

I just think that there is something that is only relevant to
C++ in the OP.

Sep 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.