0

I am trying to understand how C++11 handles strong reference count on control block of a shared pointer. I am comfortable with idea of decreasing it by one whenever destructor is called.

My problem is with incrementing the strong reference count on control block, which is dynamically allocated, during run-time. This looks even more complicated to me in case of multiple run-time contexts.

How a program can find the same strong reference count from a different context?

Edit: My problem was not with implementation of shared_ptr. It was about using it in multi process/thread/context environment.

CanCode
  • 154
  • 2
  • 10
  • What exactly do you mean by *"incrementing the strong reference count on control block"*? – nwp Mar 12 '18 at 21:15
  • Not sure I understand the problem - the constructor increments the reference count by 1, the destructor decreases it by 1 – UnholySheep Mar 12 '18 at 21:16
  • @nwp "the number of shared_ptrs that own the managed object" as stated in http://en.cppreference.com/w/cpp/memory/shared_ptr – CanCode Mar 12 '18 at 21:18
  • The only way to increment a reference count is by copying the `shared_ptr`. You know the reference count because you got it from the `shared_ptr` you are copying. – nwp Mar 12 '18 at 21:20
  • @nwp I am wondering how this is achieved in the case of multiple processes or multi-thread run-time. Even using mutex::lock structures, I could not come up with a proper implementation. Do you know how language implements this? – CanCode Mar 12 '18 at 21:25
  • All copies of a shared pointer reference the same control block - is that the bit you're missing? – Toby Speight Mar 12 '18 at 21:26
  • @TobySpeight I am aware of that. But could not figure out how language decide in run-time copying shared_ptr instead of trying to create a new one when I try to create a new shared_ptr from a different process/thread? – CanCode Mar 12 '18 at 21:32
  • 1
    Possible duplicate of [How is the std::tr1::shared\_ptr implemented?](https://stackoverflow.com/questions/9200664/how-is-the-stdtr1shared-ptr-implemented) – nwp Mar 12 '18 at 21:39
  • When you "copy" a shared pointer (construct a new `std::shared_ptr` from an existing one, or assign a new value to an existing instance), then the counter (in the control block) is incremented, and there's one more instance sharing ownership of that control block (and thus of the pointed-to resource). It doesn't matter which thread causes the counter to increment (it's normally implemented as an atomic integer type). – Toby Speight Mar 12 '18 at 21:42

1 Answers1

0

How a program can find the same strong reference count from a different context?

It can't. Not from a different context, nor even from the same context. You see, it's not enough to have the address of the original object to find that reference count - you actually have to have the address of the reference count itself. In other words, you need the value of the std::shared_ptr object!

So, to have multiple contexts / objects / whatever "participate" in referencing a T object, you need to pass copies of the same shared_ptr<T> instance to them. They won't find it out for themselves nor be able to generate it.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Thank you for detailed explanation. I also discussed this issue with my OS prof today and understand that this should be concerned while designing the language. – CanCode Mar 14 '18 at 11:53