Is this Smart Pointer class thread-safe?

P

Protoman

I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************
#pragma once

class SmrtPtrDB
{
public:
SmrtPtrDB(int status=1):num(status){}
SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.num){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--;}
int status(){return num;}
private:
int num;
};

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************
#pragma once
#include <iostream>
#include <algorithm>
#include "SmrtPtrDB.hpp"
using std::cout;
using std::endl;


class NullPtr{};

template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr<T>(T* obj=0):ptr(obj),DataBase(new SmrtPtrDB){}
SmrtPtr<T>(const SmrtPtr<T>& rhs):ptr(rhs.ptr),DataBase(new
SmrtPtrDB(rhs.DataBase->status()))
{DataBase->add();}
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
void operator=(T* val)
{
SmrtPtr<T> temp(val);
swap(temp);
}
SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs)
{
SmrtPtr<T> temp(rhs);
swap(temp);
return *this;
}
bool operator==(const SmrtPtr<T>& rhs)const{if(ptr==rhs.ptr)return
true;else return false;}
bool operator!=(const SmrtPtr<T>& rhs)const{if(ptr!=rhs.ptr)return
true;else return false;}
bool operator<=(const SmrtPtr<T>& rhs)const{if(ptr<=rhs.ptr)return
true;else return false;}
bool operator>=(const SmrtPtr<T>& rhs)const{if(ptr>=rhs.ptr)return
true;else return false;}
int status(){return DataBase->status();}
T& operator*()const{if(ptr==0)throw NullPtr();else return *ptr;}
T* operator->()const{if(ptr==0)throw NullPtr();else return ptr;}
operator T*()const{if(ptr==0)throw NullPtr();else return ptr;}
private:
void swap(SmrtPtr<T>& rhs)
{
std::swap(DataBase,rhs.DataBase);
std::swap(ptr,rhs.ptr);
}
mutable SmrtPtrDB* DataBase;
T* ptr;
};

Is this the right way to implement ref-counting? Any input would be
greatly appreciated. Thanks!!!!!
 
A

Alan Johnson

Protoman said:
I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************

Sorry, I couldn't make a copy to compile and test because I might get sued.
 
P

Protoman

Alan said:
Sorry, I couldn't make a copy to compile and test because I might get sued.

I hereby authorize you to use this code for a one-time test. After
that, you must purge your computer memory, and also your short-term and
long-term memory of this code. I won't sue YOU; that was just in case a
software company, ie MS, tried to use it in production code. Test away.
 
Y

yarema

I'd like to now if this smart pointer class if thread-safe: No, it's not.
class SmrtPtrDB
Why do you need this? Can't you keep the reference count inside your
SmartPtr?
SmrtPtrDB(int status=1):num(status)
Why do you need the parameter here?
 
J

Jeremy Brown

Protoman said:
I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************
#pragma once

class SmrtPtrDB
{
public:
SmrtPtrDB(int status=1):num(status){}
SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.num){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--;}
int status(){return num;}
private:
int num;
};

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************
#pragma once
#include <iostream>
#include <algorithm>
#include "SmrtPtrDB.hpp"
using std::cout;
using std::endl;


class NullPtr{};

template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr<T>(T* obj=0):ptr(obj),DataBase(new SmrtPtrDB){}
SmrtPtr<T>(const SmrtPtr<T>& rhs):ptr(rhs.ptr),DataBase(new
SmrtPtrDB(rhs.DataBase->status()))
{DataBase->add();}
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
void operator=(T* val)
{
SmrtPtr<T> temp(val);
swap(temp);
}
SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs)
{
SmrtPtr<T> temp(rhs);
swap(temp);
return *this;
}
bool operator==(const SmrtPtr<T>& rhs)const{if(ptr==rhs.ptr)return
true;else return false;}
bool operator!=(const SmrtPtr<T>& rhs)const{if(ptr!=rhs.ptr)return
true;else return false;}
bool operator<=(const SmrtPtr<T>& rhs)const{if(ptr<=rhs.ptr)return
true;else return false;}
bool operator>=(const SmrtPtr<T>& rhs)const{if(ptr>=rhs.ptr)return
true;else return false;}
int status(){return DataBase->status();}
T& operator*()const{if(ptr==0)throw NullPtr();else return *ptr;}
T* operator->()const{if(ptr==0)throw NullPtr();else return ptr;}
operator T*()const{if(ptr==0)throw NullPtr();else return ptr;}
private:
void swap(SmrtPtr<T>& rhs)
{
std::swap(DataBase,rhs.DataBase);
std::swap(ptr,rhs.ptr);
}
mutable SmrtPtrDB* DataBase;
T* ptr;
};

Is this the right way to implement ref-counting? Any input would be
greatly appreciated. Thanks!!!!!
I don't have the book handy at the moment... but "More Effective C++" by
Scott Meyers dedicates a section or two to Smart Pointers and Reference
Counting, worth looking into...
 
M

mlimber

Jeremy said:
Protoman said:
I'd like to now if this smart pointer class if thread-safe:
[snip]
I don't have the book handy at the moment... but "More Effective C++" by
Scott Meyers dedicates a section or two to Smart Pointers and Reference
Counting, worth looking into...

So does chapter 7 of _Modern C++ Design_ by Alexandrescu, which also
deals with multithreading issues. In fact, the chapter is online for
free:

http://www.informit.com/articles/printerfriendly.asp?p=25264&rl=1

There is also a series of four columns by Alexandrescu and Held from
CUJ that revisits the design from _MC++D_:

http://www.ddj.com/dept/cpp/184403875

Cheers! --M
 
M

mlimber

Protoman wrote:
[snip illegible code]
Is this the right way to implement ref-counting? Any input would be
greatly appreciated.

Here's my input: format your code so it's readable by others.
Whitespace does not slow your software down.

Cheers! --M
 
D

Diego Martins

Protoman said:
I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************

[snip student code]

why on the earth anyone would copy that bunch of crap?

save your threats to something more valuable

and how I will got sued if I copy your "little precious" code into my
softwares? you don't seem to have the reverse engineering profile

you don't even know about critical sections. better finish the
operating system class or become a lawyer before trying to sue the
people ;-)
 
P

Protoman

Diego said:
Protoman said:
I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************

[snip student code]

why on the earth anyone would copy that bunch of crap?

save your threats to something more valuable

and how I will got sued if I copy your "little precious" code into my
softwares? you don't seem to have the reverse engineering profile

you don't even know about critical sections. better finish the
operating system class or become a lawyer before trying to sue the
people ;-)

There's ALWAYS a possibility. And besides, I'm a HS Freshman. And I
REALLY need to take some CE and Advanced C++ classes at CSULB, don't I?
 
W

W Marsh

Diego said:
Protoman said:
I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************

[snip student code]

why on the earth anyone would copy that bunch of crap?

save your threats to something more valuable

and how I will got sued if I copy your "little precious" code into my
softwares? you don't seem to have the reverse engineering profile

you don't even know about critical sections. better finish the
operating system class or become a lawyer before trying to sue the
people ;-)

There's ALWAYS a possibility. And besides, I'm a HS Freshman. And I
REALLY need to take some CE and Advanced C++ classes at CSULB, don't I?

You CAN just learn stuff yourself, you know.
 
P

Protoman

W said:
Diego said:
Protoman wrote:
I'd like to now if this smart pointer class if thread-safe:

Code:

// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************

[snip student code]

why on the earth anyone would copy that bunch of crap?

save your threats to something more valuable

and how I will got sued if I copy your "little precious" code into my
softwares? you don't seem to have the reverse engineering profile

you don't even know about critical sections. better finish the
operating system class or become a lawyer before trying to sue the
people ;-)

There's ALWAYS a possibility. And besides, I'm a HS Freshman. And I
REALLY need to take some CE and Advanced C++ classes at CSULB, don't I?

You CAN just learn stuff yourself, you know.

Listen buddy, I've BEEN learning this stuff myself for the past five
YEARS. A college class would be a nice supplement to spending hours
reading reference books and comp.lang.c++.
 
T

Thomas J. Gritzan

Thomas J. Gritzan напиÑав:
You can do that for yourself. Google helps alot.

I wanted to say: It's hardly possible.

A reference counted smart pointer has to share the pointee and the
reference count with its instances, so there are two common approaches:

- intrusive: The counter is inside the pointee, the smart pointer calls
member functions like AddRef() and Release().
- non-intrusive: The smart pointer allocates an extra counter (either a
class or a simple "unsigned int")

From the Website:
[...]
private:

struct counter {
counter(X* p = 0, unsigned c = 1) : ptr(p), count(c) {}
X* ptr;
unsigned count;
}* itsCounter;
[...]

This implementation also uses an extra class for the reference count.
The difference is that the extra class is declared inside the smart
pointer class and also holds the pointer to the pointee.
If you don't it one here you can find more
http://www.codeproject.com/cpp/smartptr.asp .

This also, but it uses a policy based approach.
 
R

red floyd

Protoman said:
Diego said:
// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
[redacted]
[redacted]
There's ALWAYS a possibility. And besides, I'm a HS Freshman. And I
REALLY need to take some CE and Advanced C++ classes at CSULB, don't I?

But wait! I thought you were a rocket scientist! And your copyright
notice indicates you're a Commander in some sort of Naval service!
 
F

Frederick Gotham

Protoman posted:
Listen buddy, I've BEEN learning this stuff myself for the past five
YEARS. A college class would be a nice supplement to spending hours
reading reference books and comp.lang.c++.


Perhaps you're just stupid then. All of my programming expertise is a product
of:

(1) Learning from books.
(2) Practising.
(3) Reading and posting to comp.lang.*

From what I've seen of them, lecturers are a poor substitute for motivated
self-learning.
 
J

Joe Seigh

Protoman said:
I'd like to now if this smart pointer class if thread-safe:

Code: (snip)

Is this the right way to implement ref-counting? Any input would be
greatly appreciated. Thanks!!!!!

If you want atomically thread-safe and not merely "thread-safe as int",
take a look at
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&[email protected]

The cas64 is ibm style cas rather than msoft style cas. It's in assembler and
there might be some examples online somewhere. There used to be a version
downloadable from my sourceforge project page but it was such a good algorithm
that Sun has decided to patent it as their own invention. Seriously. Look
up patent application
20060037026 'Lightweight reference counting using single-target synchronization'
on upto.gov. There's some really nice illustrations in the patent application
though.
 
P

Protoman

Frederick said:
Protoman posted:



Perhaps you're just stupid then. All of my programming expertise is a product
of:

(1) Learning from books.
(2) Practising.
(3) Reading and posting to comp.lang.*

From what I've seen of them, lecturers are a poor substitute for motivated
self-learning.

Damn it, I *AM* self-motivated!!!! How did I spend five years learning
something w/o self-motivation?!!!?
 
F

Frederick Gotham

Protoman posted:

Damn it, I *AM* self-motivated!!!! How did I spend five years learning
something w/o self-motivation?!!!?


Then try harder. I've been programming in C++ for approximately 5 years, and
my proficiency far surpasses yours.

Get good books. Ask questions. Practise.
 
D

Default User

red said:
Protoman said:
Diego said:
// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
[redacted]
[redacted]
There's ALWAYS a possibility. And besides, I'm a HS Freshman. And I
REALLY need to take some CE and Advanced C++ classes at CSULB,
don't I?

But wait! I thought you were a rocket scientist! And your copyright
notice indicates you're a Commander in some sort of Naval service!

I killfiled this Protoman twit from a previous "visit" to the
newsgroup.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top