473,809 Members | 2,780 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 2832

Default User wrote:
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
That just a title my friends apply to me. I do plan on going to NROTC
when I'm in college. Let's just get back OT.

Aug 10 '06 #21

Protoman wrote:
Default User wrote:
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

That just a title my friends apply to me. I do plan on going to NROTC
when I'm in college. Let's just get back OT.
I would like to point out that you are impersonating a military
officer, and that I should report you to the Naval Criminal
Investigative Service. Don't copyright anything with a fake name,
unless it is a business and you have filed the proper paperwork

Aug 10 '06 #22

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
// *************** *************** *************** ***************
I would also like to point out that if you do not register your
copyright with the US Copyright Office you cannot sue over copyright.
If you would liek attorney's fees paid you must do it within 3 months
of publication, and the evidence will be considered prima facie only if
you register within 5 years of publication.

Aug 10 '06 #23

cdcarter wrote:
Protoman wrote:
Default User wrote:
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
That just a title my friends apply to me. I do plan on going to NROTC
when I'm in college. Let's just get back OT.

I would like to point out that you are impersonating a military
officer, and that I should report you to the Naval Criminal
Investigative Service. Don't copyright anything with a fake name,
unless it is a business and you have filed the proper paperwork
Never said I was a *Naval* Commander, now did I? There ARE other ways
you can get the title of "Commander" , like there are other ways of
getting the title "Colonel".

Aug 11 '06 #24
In article <11************ **********@74g2 000cwt.googlegr oups.com>,
cd******@gmail. com says...

[ ... ]
I would also like to point out that if you do not register your
copyright with the US Copyright Office you cannot sue over copyright.
OT, but yes, you can sue for infringement of an unregistered copyright.
You can't be awarded damages if you win, but you can get a court order
for the infringing party to stop. Of course, given that this newsgroup
is distributed internationally , there are undoubtedly quite a few
participants for whom the rules are entirely different, and for whom the
US copyright office is of little or no consequence.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 11 '06 #25
Never said I was a *Naval* Commander, now did I? There ARE other ways
you can get the title of "Commander" , like there are other ways of
getting the title "Colonel".

Klingon Commander?

Aug 11 '06 #26

au************* *@gmail.com wrote:
Never said I was a *Naval* Commander, now did I? There ARE other ways
you can get the title of "Commander" , like there are other ways of
getting the title "Colonel".


Klingon Commander?
Sure, why not? I like Star Trek: TNG.

Aug 11 '06 #27

Protoman wrote:
au************* *@gmail.com wrote:
Never said I was a *Naval* Commander, now did I? There ARE other ways
you can get the title of "Commander" , like there are other ways of
getting the title "Colonel".

Klingon Commander?

Sure, why not? I like Star Trek: TNG.
You are the commander of the gay pride

Aug 14 '06 #28
Diego Martins wrote:
Protoman wrote:
au************* *@gmail.com wrote:
Never said I was a *Naval* Commander, now did I? There ARE other ways
you can get the title of "Commander" , like there are other ways of
getting the title "Colonel".
>
>
Klingon Commander?
Sure, why not? I like Star Trek: TNG.

You are the commander of the gay pride
Please stop this nonsense. Comment on the code, not the person.

Cheers! --M

Aug 14 '06 #29

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
5089
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10387
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...
1
7662
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
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.