3

Is there a limit in the maximum number of std::shared_ptr managed objects per process?

Asking this because there is a maximum number of mutexes per process. If the implementation of std::shared_ptr is using mutex, isn't there a similar limit in the number of objects managed objects?

An answer discussing mutex-based and atomic-operation-base implementations is more than welcome.

Community
  • 1
  • 1
updogliu
  • 6,066
  • 7
  • 37
  • 50
  • I doubt there is any limit as per the C++ spec. There may be platform-specific limits. So, what OS/compiler are you using? – Nicu Stiurca Apr 07 '14 at 18:01
  • 5
    There is no such limit specified in the standard. Any implementation-specific limits are of course specific to the implementation ;-) (and would thus be documented in the specific standard library docs), but I'd expect most implementations to use an atomic counter instead of a mutex. – Angew is no longer proud of SO Apr 07 '14 at 18:02

1 Answers1

1

Generally no, there is no limit because the shared_ptr implementations on common OS's do not use mutexes under the hood (they use atomic increment/decrement functions). Also, an implementation could, for example, choose to manage all reference count accesses with a single mutex. Might be slow in some cases, but it could work.

Nathan Monteleone
  • 5,430
  • 29
  • 43