Re: Continuing my foray into the TR1: tr1::shared_ptr<>
"Emmanuel Deloget" <logout@free.fr> wrote in message
news:1172835752.748131.203690@8g2000cwh.googlegroups.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<foo> g_foo = new foo;
reader_threads(...) {
for(;;) {
{
shared_ptr<foo> l_foo = g_foo;
if (l_foo) {
l_foo->do_something();
}
}
// do something else
// ect, ect...
}
}
writer_thread(...) {
for(;;) {
{
shared_ptr<foo> l_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...