473,799 Members | 2,997 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 #1
28 2829
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
// *************** *************** *************** ***************
Sorry, I couldn't make a copy to compile and test because I might get sued.

--
Alan Johnson
Aug 9 '06 #2

Alan Johnson 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
// *************** *************** *************** ***************

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

--
Alan Johnson
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.

Aug 9 '06 #3
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(s tatus)
Why do you need the parameter here?

Aug 9 '06 #4
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
// *************** *************** *************** ***************
#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!!!!!
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...

Aug 9 '06 #5
Jeremy Brown wrote:
Protoman wrote:
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/pri...p?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

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

Aug 9 '06 #7

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 ;-)

Aug 9 '06 #8

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?

Aug 9 '06 #9
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.
Aug 9 '06 #10

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

Similar topics

1
1710
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
2858
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
5134
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
18212
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
5085
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
2968
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
4520
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
9541
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
10485
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...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6805
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
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.