草庐IT

my_shared_ptr

全部标签

c++ - boost shared_mutex(多次读取/一次写入)的示例?

我有一个多线程应用程序,它必须经常读取一些数据,并且偶尔会更新这些数据。现在,互斥锁可以安全地访问该数据,但它很昂贵,因为我希望多个线程能够同时读取,并且仅在需要更新时将它们锁定(更新线程可以等待其他线程完成).我认为这是boost::shared_mutex应该做的,但我不清楚如何使用它,也没有找到一个明确的例子。有没有人有一个简单的例子可以用来入门? 最佳答案 1800INFORMATION或多或少是正确的,但我想更正一些问题。boost::shared_mutex_access;voidreader(){boost::shar

C++ - 传递对 std::shared_ptr 或 boost::shared_ptr 的引用

如果我有一个需要使用shared_ptr的函数,传递一个对它的引用不是更有效吗(以避免复制shared_ptr对象)?可能的不良副作用是什么?我设想了两种可能的情况:1)在函数内部,一个参数的拷贝,就像在ClassA::take_copy_of_sp(boost::shared_ptr&sp){...m_sp_member=sp;//Thiswillcopytheobject,incrementingrefcount...}2)在函数内部只使用参数,就像在Class::only_work_with_sp(boost::shared_ptr&sp)//Again,nocopyhere{.

c++ - 如何使用带有 std::unique_ptr 成员的自定义删除器?

我有一个包含unique_ptr成员的类。classFoo{private:std::unique_ptrbar;...};Bar是具有create()函数和destroy()函数的第三方类。如果我想在一个独立的函数中使用std::unique_ptr,我可以这样做:voidfoo(){std::unique_ptrbar(create(),[](Bar*b){destroy(b);});...}有没有办法将std::unique_ptr作为类的成员? 最佳答案 假设create和destroy是具有以下签名的自由函数(这似乎是OP

c++ - 为什么 std::shared_ptr<void> 工作

我发现一些代码使用std::shared_ptr在关机时执行任意清理。起初我认为这段代码不可能工作,但后来我尝试了以下方法:#include#include#includeclasstest{public:test(){std::cout>">v;{std::cout(newtest()));std::cout这个程序给出了输出:Atbeginofmain.creatingstd::vector>CreatingtestTestcreatedLeavingscopeLeavingmainTestdestroyed我对为什么这可能有效有一些想法,这与为G++实现的std::shared_

C++ unique_ptr 和映射

我正在尝试使用C++0xunique_ptrclass在map内像这样://compilewith`g++main.cpp-std=gnu++0x`#include#include#includeusingnamespacestd;structFoo{char*str;Foo(charconst*str_):str(strdup(str_)){}};intmain(void){typedefstd::map>Bar;Barbar;autoa=bar.insert(Bar::value_type(1,newFoo("one")));return0;}但是GCC给了我以下错误(缩短,我认为

C++ unique_ptr 和映射

我正在尝试使用C++0xunique_ptrclass在map内像这样://compilewith`g++main.cpp-std=gnu++0x`#include#include#includeusingnamespacestd;structFoo{char*str;Foo(charconst*str_):str(strdup(str_)){}};intmain(void){typedefstd::map>Bar;Barbar;autoa=bar.insert(Bar::value_type(1,newFoo("one")));return0;}但是GCC给了我以下错误(缩短,我认为

c++ - 为什么我要 std::move 一个 std::shared_ptr?

我一直在看Clangsourcecode我发现了这个片段:voidCompilerInstance::setInvocation(std::shared_ptrValue){Invocation=std::move(Value);}我为什么要std::move一个std::shared_ptr?转让共享资源的所有权有什么意义吗?我为什么不这样做呢?voidCompilerInstance::setInvocation(std::shared_ptrValue){Invocation=Value;} 最佳答案 我认为其他答案没有足够强

c++ - std::auto_ptr 到 std::unique_ptr

随着新标准的到来(以及某些编译器中已经提供的部分),新类型std::unique_ptr应该是std::auto_ptr的替代品.它们的用法是否完全重叠(所以我可以对我的代码进行全局查找/替换(不是我会这样做,但如果我这样做了))或者我是否应该知道一些从阅读中不明显的差异文档?另外如果是直接替换,为什么要给它一个新名字而不是仅仅改进std::auto_ptr? 最佳答案 您无法进行全局查找/替换,因为您可以复制auto_ptr(具有已知后果),但只能移动unique_ptr。任何看起来像的东西std::auto_ptrp(newin

c++ - 为什么我不能将 unique_ptr 推回 vector 中?

这个程序有什么问题?#include#includeintmain(){std::vector>vec;intx(1);std::unique_ptrptr2x(&x);vec.push_back(ptr2x);//Thistinycommandhasaviciouserror.return0;}错误:Infileincludedfromc:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0,fromc:\mingw\bin\../lib/gcc/mingw32/4.5.0/i

c++ - boost::serialization 如何与 C++11 中的 std::shared_ptr 一起使用?

我知道有aBoostmodule对于serialization的boost::shared_ptr,但我找不到std::shared_ptr的任何内容.另外,我不知道如何轻松实现它。恐怕下面的代码namespaceboost{namespaceserialization{templateinlinevoidserialize(Archive&ar,std::shared_ptr&t,constunsignedintversion){if(Archive::is_loading::value){T*r;ar>>r;t=r;}else{ar不起作用。事实上,如果某个对象被多次引用,它会在第