草庐IT

atomic_shared_ptr

全部标签

c++ - 创建一个非线程安全的 shared_ptr

我正在开发一个多线程程序,但有一个UI组件广泛使用std::shared_ptr来管理元素。我可以保证只有一个线程会使用这些shared_ptrs。有没有一种方法可以定义一个不会产生线程安全引用计数开销的shared_ptr?它可以基于boost::shared_ptr或std::shared_ptr。编辑:感谢提到intrusive_ptr的回答。我忘了提到我还需要weak_ptr功能,所以排除了它。更新:我的答案是使用Boost中的local_shared_ptr。查看来自“他漫步”的评论 最佳答案 AndreiAlexandr

c++ - 锁定 shared_ptr

我有一个共享对象需要发送到系统API并稍后将其提取回来。系统API仅接收void*。我不能使用shared_ptr::get()因为它不会增加引用计数,并且它可能在从系统API提取之前被其他线程释放。发送一个新的shared_ptr*将起作用,但涉及额外的堆分配。一种方法是让对象派生自enable_shared_from_this。但是,由于此类模板仅拥有一个weak_ptr,因此不足以防止对象被释放。所以我的解决方案如下所示:classMyClass:publicenable_shared_from_this{private:shared_ptrm_this;public:void*

c++ - 多个 shared_ptr 存储相同的指针

考虑这个程序:#include#includeclassX:publicstd::enable_shared_from_this{public:structCleanup1{voidoperator()(X*)const;};structCleanup2{voidoperator()(X*)const;};std::shared_ptrlock1();std::shared_ptrlock2();};std::shared_ptrX::lock1(){std::cout(this,Cleanup1());}std::shared_ptrX::lock2(){std::cout(this

c++ - 为什么 unique_ptr 不能推断删除器的类型?

假设我想使用带有unique_ptr的自定义删除器:voidcustom_deleter(int*obj){deleteobj;}为什么我要这样写:std::unique_ptrx(newint,custom_deleter);而不是这个:std::unique_ptrx(newint,custom_deleter);//doesnotcompile?不能推断删除器的类型吗? 最佳答案 对于unique_ptr,删除器是类型的一部分:template>classunique_ptr;因此,当您构造一个对象时,您需要指定它的类型。你正

c++ - 'managed_shared_memory' 应该分配多少内存? ( boost )

我正在寻找关于在通过boost::interprocess的managed_shared_memory创建静态共享内存块时应该分配多少内存的明确答案(如果确实存在的话)。连officialexamples似乎分配arbitrarilylarge内存块。考虑以下结构://Example:simplestructwithtwo4-bytefieldsstructPoint2D{intx,y;};我最初的react是必要的大小是8个字节,或sizeof(Point2D)。当我尝试构造一个对象时,这惨遭失败,在运行时出现段错误。//BAD:8bytesisnowherenearenoughme

c++ - 如何在 DDD(或 gdb)中使用 unique_ptr 调试 C++11 代码?

std::unique_ptr很好,但我发现在DDD中调试时不太舒服或gdb.我正在使用作为gcc一部分的gdbpretty-print(例如,/usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py)。这是可读性的一大胜利,例如:$printpTeststd::unique_ptrcontaining0x2cef0a0但是,取消引用指针不起作用:$print*pTestCouldnotfindoperator*.当我需要访问该值时,我必须手动复制指针并将其转换为正确的类型,例如:print*((MyType*)0x2cef0a0)如果进

c++ - 将 reinterpret_cast 输入重新解释为 std::unique_ptr 永远不会真正安全吗?

当使用具有可变大小结构(必须分配为byte[]然后转换为结构)的各种API时,如果unique_ptr持有者可以指向该结构,那将是很好的,因为这就是我们将要做的正在使用。例子:std::unique_ptrv;v.reset(reinterpret_cast(newBYTE[bytesRequired]));这允许`v提供结构本身的View,这是更可取的,因为我们不需要第二个变量,除了删除之外我们不关心字节指针。问题在于可能会在强制转换上对指针进行thunk(使其释放不安全)。我看不出为什么编译器会在cast上更改指针值(因为没有继承),但我听说标准保留对任何cast上的任何指针进行t

c++ - 使用别名模板时无法将 `std::unique_ptr` 分配给 clang 中的基类

以下代码在gcc4.9.3和clang3.7.1上编译和运行得很好//std::unique_ptr#include//Templateclassfortemplate-templateargumentstemplatestructBar{};//BaseclasstemplateclassXX>structBase{};//DerivedclassthatoperatesonlyonBartemplatestructDerived:publicBase{};//Holdstheunique_ptrtemplateclassXX>structFoo{std::unique_ptr>fo

c++ - 我们什么时候应该使用 std::enable_shared_from_this

我刚知道std::enable_shared_from_this表格thislink.但是看了下面的代码,不知道什么时候用。try{Goodnot_so_good;std::shared_ptrgp1=not_so_good.getptr();}catch(std::bad_weak_ptr&e){//undefinedbehavior(untilC++17)andstd::bad_weak_ptrthrown(sinceC++17)std::cout上面的代码“不太好”,因为不存在shared_ptr打电话前getptr().所以好的应该是:std::shared_ptrgp1=st

c++ - 如何优雅地初始化 std::atomic 数组?

假设我有一个包含std::atomic成员数组的类,其中数组的大小是通过计算确定的(即它可能会根据程序中其他地方的其他常量而改变):classFoo{staticconstexprsize_tkArraySize=ComputeArraySize();std::atomicatomics_[kArraySize];};什么是最优雅的方式来确保原子都被初始化为零?我能比在Foo的构造函数中遍历数组更好吗?显式存储零?std::array的答案是否不同?通常我会在这里使用大括号初始值设定项,但是导出的长度(可能很长)使它变得困难。请注意,我不能假设Foo的实例具有静态存储持续时间。