473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Continuing my foray into the TR1: tr1::shared_ptr<>

Hello y'all,

I'm currently writing a serie of blog tickets about the TR1, both from
a definition point of view and from an implementation point of view.
The next iteration in the series deals with the smart pointer sub-
library in the TR1 (weak_ptr, shared_ptr, enable_shared_from_this),
and guess what, I have a few question about the implementation of all
this.

Before I ask my question, I'd like to say that I studied all the
shared_ptr implementation I was able to study (namely,
boost::shared_ptr by Peter Dimov and the tr1 implementation of g++,
which is based on boost's implementation). Unfortunately, I still
don't understand something.

The TR1 says about the weak_ptr::expire() method:

bool expired() const;
Returns: use_count() == 0.
Throws: nothing.
[Note: expired() may be faster than use_count(). -end note]

The problems is about how it detects use_count() = 0. Let's examine
this code:

shared_ptr<Asp(new A);
weak_ptr<Awp(sp);
sp.reset();

This will transform sp into an empty shared_ptr. Typical
implementation will implement reset as:

void reset()
{
shared_ptr<T>().swap(*this);
}

This will destroy the pointer associated to sp (because the temp
object will go out of scope), and this will also destroy the internal
reference counter, as it is no more needed (shared_count = 0).

Question: if this internal reference counter is destroyed, how can
use_count() (and, of course) expired() be implemented so that they
don't crash?

Thanks for your patience ;)

Mar 2 '07 #1
3 3203
"Emmanuel Deloget" <lo****@free.frwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
Hello y'all,

I'm currently writing a serie of blog tickets about the TR1, both from
a definition point of view and from an implementation point of view.
Here is a fairly efficient way to implement shared_ptr:

http://appcore.home.comcast.net/vzoom/refcount/
(I expose a different interface than shared_ptr, however, it would be
trivial to make my algorithm expose it as well.)

Also, when you use something like the algorithm I created you get the added
benefit of strong thread safety. Currently, shared_ptr cannot handle strong
competing access from multiple threads... In other words, the following
scenario is a no go with current shared_ptr algorithm, and perfectly legal
with my prototype code:

// pseudo-code

static shared_ptr<foog_foo = new foo;

reader_threads(...) {
for(;;) {
{
shared_ptr<fool_foo = g_foo;
if (l_foo) {
l_foo->do_something();
}
}
// do something else
// ect, ect...
}
}

writer_thread(...) {
for(;;) {
{
shared_ptr<fool_foo = g_foo.xchg(new foo);
if (l_foo) {
l_foo->do_something();
}
}

// do something else
// ect, ect...
}
}


Does anybody think that shared_ptr should be able to handle a scenario like
the above? If you think so, my prototype implementation should help inspire
some other possible designs...
Mar 2 '07 #2
On 2 mar, 13:00, "Chris Thomasson" <cris...@comcast.netwrote:
Does anybody think that shared_ptr should be able to handle a scenario like
the above? If you think so, my prototype implementation should help inspire
some other possible designs...
This is the basic difference between the C++ standard and the real
world: as of today, the standard has no idea of what a thread is (and
as a consequence, it doesn't need to be thread-safe, although some
implementations are). It seems that the the next iteration of the C++
standard is going to introduce a thread library, so such scenario is
likely to happen.

I'm going to check your implementation soon - thanks ;)

-- Emmanuel Deloget
Mar 2 '07 #3
"Emmanuel Deloget" <lo****@free.frwrote in message
news:11********************@31g2000cwt.googlegroup s.com...
On 2 mar, 13:00, "Chris Thomasson" <cris...@comcast.netwrote:
>Does anybody think that shared_ptr should be able to handle a scenario
like
the above? If you think so, my prototype implementation should help
inspire
some other possible designs...

This is the basic difference between the C++ standard and the real
world: as of today, the standard has no idea of what a thread is (and
as a consequence, it doesn't need to be thread-safe, although some
implementations are).
:^)

This is why I had to implement about 99.99% of my reference counting
algorithm with pure* 32-bit x86 assembly language:

http://appcore.home.comcast.net/vzdo...c/static-init/
(more on this in section 2...)

Whoa! That's some pretty tricky stuff huh? ;^)

It seems that the the next iteration of the C++
standard is going to introduce a thread library, so such scenario is
likely to happen.
Indeed! FWIW, and IMHO, I sure do think that Standard C++ would do well by
providing an atomically thread-safe smart-pointer with the ability to handle
any "strong competing accesses" a end-user application can throw at it. This
new shared_ptr would end up being more useful and flexible. For instance,
you could use it for a whole new class of multi-threaded reader-writer
patterns which were previously impossible to implement using only
shared_ptr.

Darn... I hope I don't ending up sounding like a salesman or something!

lol. :^)

I'm going to check your implementation soon - thanks ;)
If you are having any trouble understanding the assembly language, let me
know and I will post my entire algorithm in the form of some fairly simple
pseudo-code. Well, here it is anyway since a lot of people get headaches
after reading basically any amount of assembly! ;^)
http://article.gmane.org/gmane.comp....t.devel/149803
(pseudo-code with a fairly detailed explanation...)
http://search.gmane.org/?query=&auth...+thomasson---A
(contains a rather robust discussion about the algorithm over on the boost
developer list...)
Enjoy! :^)
* - I did not use any inline assembly. IMHO, it's safer to use a "real"
assembler to do this sort of stuff... For instance, instead of using inline
asm in GCC, you should probably be using GAS instead...
Mar 3 '07 #4

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

Similar topics

1
by: red floyd | last post by:
In the August 2004 CUJ, Sutter&Hyslop's column implies that shared_ptr<> has become part of the standard. Is the revised standard finalized? Am I correct in assuing that shared_ptr<> will be...
10
by: Piotr | last post by:
I have a class 'Statistics' which has a private attribute ' vector<int>* _x;' And in the destructor of the Statistics, I have this code to free the memory: Statistics::~Statistics() { if...
3
by: tomix | last post by:
Hi, I have a domain model written with STL and a client which uses it I would like to give the client a better way to declare a container. Where is the best place to put all my typedefs? (typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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 project—planning, coding, testing,...
0
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...

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.