473,809 Members | 2,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this Smart Pointer class thread-safe?

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(s tatus){}
SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.nu m){}
~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>(cons t SmrtPtr<T>& rhs):ptr(rhs.pt r),DataBase(new
SmrtPtrDB(rhs.D ataBase->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<Ttemp(v al);
swap(temp);
}
SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs)
{
SmrtPtr<Ttemp(r hs);
swap(temp);
return *this;
}
bool operator==(cons t SmrtPtr<T>& rhs)const{if(pt r==rhs.ptr)retu rn
true;else return false;}
bool operator!=(cons t SmrtPtr<T>& rhs)const{if(pt r!=rhs.ptr)retu rn
true;else return false;}
bool operator<=(cons t SmrtPtr<T>& rhs)const{if(pt r<=rhs.ptr)retu rn
true;else return false;}
bool operator>=(cons t SmrtPtr<T>& rhs)const{if(pt r>=rhs.ptr)retu rn
true;else return false;}
int status(){return DataBase->status();}
T& operator*()cons t{if(ptr==0)thr ow NullPtr();else return *ptr;}
T* operator->()const{if(ptr ==0)throw NullPtr();else return ptr;}
operator T*()const{if(pt r==0)throw NullPtr();else return ptr;}
private:
void swap(SmrtPtr<T> & rhs)
{
std::swap(DataB ase,rhs.DataBas e);
std::swap(ptr,r hs.ptr);
}
mutable SmrtPtrDB* DataBase;
T* ptr;
};

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

Aug 9 '06
28 2831
ya****@gmail.co m schrieb:
>class SmrtPtrDB
Why do you need this? Can't you keep the reference count inside your
SmartPtr?
Show me a working solution, please! :-)

--
Thomas
Aug 9 '06 #11

Thomas J. Gritzan написав:
ya****@gmail.co m schrieb:
class SmrtPtrDB
Why do you need this? Can't you keep the reference count inside your
SmartPtr?

Show me a working solution, please! :-)

--
Thomas
You can do that for yourself. Google helps alot.

For example, this one http://ootips.org/yonat/4dev/counted_ptr.h ? If
you don't it one here you can find more
http://www.codeproject.com/cpp/smartptr.asp .

Aug 9 '06 #12

W Marsh wrote:
On 9 Aug 2006 13:46:03 -0700, "Protoman" <Pr**********@g mail.com>
wrote:

Diego Martins wrote:
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++.

Aug 9 '06 #13
ya****@gmail.co m schrieb:
Thomas J. Gritzan написав:
>ya****@gmail.co m schrieb:
>>>class SmrtPtrDB
Why do you need this? Can't you keep the reference count inside your
SmartPtr?
Show me a working solution, please! :-)
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")
For example, this one http://ootips.org/yonat/4dev/counted_ptr.h ?
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Aug 9 '06 #14
Protoman wrote:
Diego Martins wrote:
>>>
// 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!
Aug 9 '06 #15
Protoman posted:
>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++.

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.

--

Frederick Gotham
Aug 9 '06 #16
Protoman wrote:
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=e...8%40xemaps.com

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.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Aug 9 '06 #17

Frederick Gotham wrote:
Protoman posted:
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++.


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.

--

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

Aug 9 '06 #18
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?!!!?

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.

--

Frederick Gotham
Aug 9 '06 #19
red floyd wrote:
Protoman wrote:
Diego Martins wrote:

// 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
Aug 9 '06 #20

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

Similar topics

1
1711
by: Sreeram | last post by:
Hello, I am having a doubt about smart pointer. I wrote a smart ptr class with limited functionalities. I want to do like this! CSmartPtr <CSomeClass> test = new CSomeClass; // here i need to call a function which will start a thread and returns once it starts the thread. Here i should able to increment the refcount of smart pointer and should decrement once the thread finishes executing. What i
6
1807
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles, various books and ... whatever I could find on the subject :-) I'll leave out the reference counter part because its pretty basic, but my reference counter class is called xObject. My smart pointer class is called xPointer.
3
1914
by: Vijai Kalyan | last post by:
I have been thinking about this and it may have already been thrashed out and hung out to dry as a topic of no more interest but here goes. I found when implementing a smart pointer that the typical implementation goes like: template<typename T> class SmartPointer { // other stuff
4
2859
by: Matthias Kaeppler | last post by:
Hi, I'm having a hard time figuring out how I can initialize a smart pointer based on a certain condition: if something then ptr = 0; // init with NULL else ptr = new XYZ; // init with a sane value endif
5
7806
by: Boris | last post by:
I've a class C with a smart pointer (I use boost::shared_ptr) which is initialized in the constructor: class C { boost::shared_ptr<D> d; public: C() : d(new d()) { } }; When the program starts class C is instantiated quite a lot. As all the
92
5141
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
14
18215
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
33
5088
by: Ney Andr de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
10
2970
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
50
4523
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.