472,782 Members | 1,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 2702
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

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.