shared-ptr

Is it thread safe to reset and copy shared_ptr simultaneously?

旧城冷巷雨未停 提交于 2021-02-17 21:50:41
问题 Boost documentation describes shared pointer's behavior when accessing it from multiple threads simultaneously. Particularly they give some examples: shared_ptr<int> p(new int(42)); //--- Example 1 --- // thread A shared_ptr<int> p2(p); // reads p // thread B shared_ptr<int> p3(p); // OK, multiple reads are safe //--- Example 2 --- // thread A p.reset(new int(1912)); // writes p // thread B p2.reset(); // OK, writes p2 //--- Example 3 --- // thread A p = p3; // reads p3, writes p // thread B

Why does libc++'s implementation of shared_ptr use full memory barriers instead of relaxed?

别说谁变了你拦得住时间么 提交于 2021-02-17 12:22:38
问题 In boost's implementation of shared_ptr , it uses relaxed memory ordering to increment its reference count. This appears safe as decrements use acquire/release to make sure that any previous decrements are visible to the thread before releasing memory. This method seems correct and appears in Herb Sutters talk on atomics In libc++'s implementation uses full memory barriers template <class T> inline T increment(T& t) _NOEXCEPT { return __sync_add_and_fetch(&t, 1); } template <class T> inline T

Why does shared_ptr<T>::use_count() return a long instead of an unsigned type?

别来无恙 提交于 2021-02-15 12:07:38
问题 shared_ptr observers 20.8.2.2.5 C++14 Final Draft (n4296) long use_count() const noexcept; Returns: the number of shared_ptr objects, *this included, that share ownership with *this , or 0 when *this is empty. [Note: use_count() is not necessarily efficient. — end note] 回答1: According to this page http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html The return type of use_count is signed to avoid pitfalls such as p.use_count() > -1 evaluating to false. with a reference to John

std::shared_ptr initialized with other shared_ptr data

浪子不回头ぞ 提交于 2021-02-11 18:15:54
问题 I'm using recently new C++11 features such as std::shared_ptr, because I am converting a C code into Classes and in this code there is an heavy usage of "old-fashioned" pointers. I have one doubt: in a method of a class, where i use local shared_ptr, can i initialized them with the data of the passed reference to another smart_ptr and then modify the data? Example: void myFunction(std::shared_ptr<T> &my_reference) { std::shared_ptr<T> my_local_ptr(my_reference.get()); /* Doing stuff on local

std::shared_ptr initialized with other shared_ptr data

跟風遠走 提交于 2021-02-11 18:12:52
问题 I'm using recently new C++11 features such as std::shared_ptr, because I am converting a C code into Classes and in this code there is an heavy usage of "old-fashioned" pointers. I have one doubt: in a method of a class, where i use local shared_ptr, can i initialized them with the data of the passed reference to another smart_ptr and then modify the data? Example: void myFunction(std::shared_ptr<T> &my_reference) { std::shared_ptr<T> my_local_ptr(my_reference.get()); /* Doing stuff on local

how to block usage of std::make_shared<T>

橙三吉。 提交于 2021-02-07 21:54:46
问题 I know I can prevent ordinary heap allocation of custom class and its descendants by making the class's operator new private, but is there any way to prevent a user of a library from calling std::make_shared on a custom class (or its descendants)? Apparently, simply making the operator new private in a class does not stop it. Note that I do not want to completely prevent shared pointers from being created by any means, as I intend to still be able to produce a std::shared_ptr for my custom

Better shared_ptr by distinct types for “ownership” and “reference”?

*爱你&永不变心* 提交于 2021-02-05 10:44:05
问题 I would like to use a pointer-like object Ownership<Type> m_foo for the owning object and handle Reference<Type> m_someFoo as a classical "pointer" in another context, whereas my Reference should know when the original object does not exist anymore (e.g. by returning nullptr) and it should furthermore be possible to prevent the original object from deletion for a small period of time (locking). I know that shared_ptr (Ownership) and weak_ptr (Reference) provide similar functionality. However,

Can smart pointers be implicitly used as pointers?

安稳与你 提交于 2021-01-28 20:18:42
问题 Are smart pointers considered as pointers? And thus can they implicitly used as pointers? Let's say I have the following class: class MyClass { //... std::shared_ptr<AnotherClass> foo() { /*whatever*/ }; void bar(AnotherClass* a) { /*whatever too*/ }; //... } Then can I use MyClass the following way? // m is an instance of MyClass m.bar(m.foo()); 回答1: NO! It would be a terrible API . Yes, you could easily implement it within shared_ptr , but just because you could doesn't mean you should. Why

Using boost::function with a parameter to shared pointer to derived class

梦想的初衷 提交于 2021-01-28 03:22:34
问题 Using C++ with g++ 5.4.0 on Ubuntu 16.04. I have a class A, and a class B that derives from class A. Function f1 takes a shared pointer to class A as parameter. Function f2 takes a shared pointer to class B as parameter and return the same type as f1. Using boost::function, another function F takes a function such as f1 as parameter. The code looks like this : result_t f1 ( const boost::shared_ptr<A> a ); result_t f2 ( const boost::shared_ptr<B> b ); typedef boost::function < result_t (const

Should my API functions take shared_ptr or weak_ptr

[亡魂溺海] 提交于 2021-01-27 05:04:27
问题 I am currently designing an API and I am not sure whether my functions should take shared_ptr or weak_ptr . There are widgets that contain viewers. The viewers have a function add_painter which adds a painter to the viewer. When a viewer needs to redraw, it uses its painters to draw into a buffer and displays the result. I came to the conclusion that the viewers should hold the painters using weak_ptr : A painter may be used in multiple viewers, so the viewer cannot own the painter. Deleting