Lock-Free Atomic Shared Pointers Without a Split Reference Count? It Can Be Done!danielanderson.net What we’ll learn today • How shared_ptr is implemented under the hood • Atomics and concurrency patterns • How existing atomic<shared_ptr> are implemented (the split reference count technique) used for • You’ve heard of shared_ptr Things we won’t cover • Alias pointers, weak pointers4 Daniel Anderson -- danielanderson.net What we’ll learn today • How shared_ptr is implemented under the the hood • Atomics and concurrency patterns • How existing atomic<shared_ptr> are implemented (the split reference count technique) • Deferred reclamation, i.e., garbage collection in C++ Some assumed0 码力 | 45 页 | 5.12 MB | 6 月前3
C++高性能并行编程与优化 - 课件 - 02 现代 C++ 入门:RAII 内存管理针。比如右边这样: 更智能的指针: shared_ptr • 使用起来很困难的原因,在于 unique_ptr 解决重复释放 的方式是禁止拷贝,这样虽然有效率高的优势,但导致使 用困难,容易犯错等。 • 相比之下, 牺牲效率换来自由度的 shared_ptr 则允许 拷贝,他解决重复释放的方式是通过引用计数: 1. 当一个 shared_ptr 初始化时,将计数器设为 1 。 2. 当一个 shared_ptr 被拷贝时,计数器加 1 。 3. 当一个 shared_ptr 被解构时,计数器减 1 。减到 0 时 ,则自动销毁他指向的对象。 • 从而可以保证,只要还有存在哪怕一个指针指向该对象 ,就不会被解构。 更智能的指针: shared_ptr (续) • 我们可以使用 p.use_count() 来获取当前 指针的引用计数,看看他是不是在智能地 增减引用计数器。 增减引用计数器。 注意 p.func() 是 shared_ptr 类型本身的成 员函数,而 p->func() 是 p 指向对象(也 就是 C )的成员函数,不要混淆。 不影响 shared_ptr 计数:弱引用 weak_ptr • 有时候我们希望维护一个 shared_ptr 的弱引用 weak_ptr ,即:弱引用的拷贝与解构不影响其 引用计数器。 • 之后有需要时,可以通过 lock()0 码力 | 96 页 | 16.28 MB | 1 年前3
FlexClassssstd::make_shared(n) std::shared_ptr (block) Control Block T T T T T …my::shared_ptr Control Block T T T T T T … int ref_cnt; int size; my::shared_ptr (block)I will write it myself // Cast and initialize the control block // Cast and initialize each T // return my::shared_ptr (block); } // FixMe: What about alignment? // ToDo: Are these reinterpret_casts safe? // CheckThis: T> auto my::make_shared(int n) { Block * block = fc::make >(n) // return my::shared_ptr (block); } (n, 0); https://godbolt.org/z/v3hdhYnaT auto begin = block->data.begin(); auto 0 码力 | 8 页 | 957.56 KB | 6 月前3
C++20: An (Almost) Complete OverviewStephan T. Lavavej Tuesday, September 15 • 13:30Concurrency Changes35 Atomic Smart Pointers Is shared_ptr thread safe? Yes: control block manipulation thread safe guarantees object is deleted exactly Error-prone, easy to accidently not use global non-member atomic operations C++20: atomic<shared_ptr> Might use mutex internally Global non-member atomic operations are deprecated36 Atomic T t; shared_ptr next; }; atomic_shared_ptr head; // in C++11: remove "atomic_" and use special // functions every time you touch head public: class reference { shared_ptr 0 码力 | 85 页 | 512.18 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - Zeno 中的现代 C++ 最佳实践 ,就可以对猫和狗都适用,实现代码的复用( dont-repeat-yourself ), 也让函数的作者不必去关注点从猫和狗的其他具体细节,只需把握住他们统一具有的“吃”这个接口。 小知识: shared_ptr 如何深拷贝? 浅拷贝: 深拷贝: 思考:能不能把拷贝构造函数也作为虚函数? • 现在我们的需求有变,不是去对同一个对象调用两次 eatTwice ,而是先把对象复制一份 拷贝,然后对对象本身和他的拷贝都调用一次 IObjectClone 模板 类。其模板参数是他的派生类 Derived 。 • 然后在这个 IObjectClone 里实现 clone 即可。那为什么需要派生类作为模板参数 ? • 因为 shared_ptr 的深拷贝需要知道对象具 体的类型。注意这里不仅 make_shared 的参数有 Derived , this 指针(原本是 IObjectClone const * 类型)也需要转化成 个端口上,比如 {“PrimitiveCreate”, “prim”} 就表示这个端口连接了 PrimitiveCreate 节点的 prim 输出端口。 • ( zany 是 shared_ptr的缩 写) 一个节点的定义,以 MakeBoxPrimitive 为例 MaxBoxPrimitive 节点的内部: apply 的定义 通过 get_input (“name”) 0 码力 | 54 页 | 3.94 MB | 1 年前3
C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串所以 cpp 之父曾经说,他设计 cpp11 的时候,是考虑“如何在对语言本身改动最小的情况下 ,尽量只在标准库里做手脚,尽可能只利用现有的语言特性,实现 cpp 的现代化。” • 例如 shared_ptr 可以通过利用语言本身的“拷贝构造函数”实现引用计数,没必要在编译器里 开洞。但“移动语义”这个概念在旧 cpp 里没有,所以这个是真正必要的语言本身的改动。 • 而 java 就是在语言层面,直接在 举例:常见容器及其相应的弱引用 强引用 弱引用 string string_view wstring wstring_view vectorspan unique_ptr T * shared_ptr weak_ptr 字符串用 substr 切片 • 熟悉 Python 的同学对切片 (slice) 操作肯定不陌生,例如: • “hello”[1:1+3] 会得到 “ ell” 0 码力 | 162 页 | 40.20 MB | 1 年前3
Making Libraries Consumable for Non-C++ DevelopersGarbage collection is really “automatic memory management”. - Reference counted - C++ – std::shared_ptr- Python - Objective-C (manual or automatic – see ARC) - Swift - COM – AddRef()/Release() 0 码力 | 29 页 | 1.21 MB | 6 月前3
C++20 STL Features: 1 Year of Development on GitHubExtensible to user-defined contiguous ranges25 And More!26 So Many New Features, Including: • atomic<shared_ptr>, atomic > • GH-601 by AdamBucior • (bit_cast, rotating/counting, power-of-2) 0 码力 | 45 页 | 702.09 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 04 从汇编角度看编译器优化new/delete 的容器:我是说,内存分配在堆上的容器 • 存储在堆上(妨碍优化): • vector, map, set, string, function, any • unique_ptr, shared_ptr, weak_ptr • 存储在栈上(利于优化): • array, bitset, glm::vec, string_view • pair, tuple, optional, variant0 码力 | 108 页 | 9.47 MB | 1 年前3
共 9 条
- 1













