草庐IT

shared_mutex

全部标签

c++ - 如何使用 shared_ptr 并从 enable_shared_from_this 继承来制作克隆方法

我已经看到编写返回boost::shared_ptr的克隆方法的一种有用方法是做classA{public:shared_ptrClone()const{return(shared_ptr(CloneImpl()));}protected:virtualA*CloneImpl()const{return(newA(*this));}};classB:publicA{public:shared_ptrClone()const{return(shared_ptr(CloneImpl()));}protected:virtualB*CloneImpl()const{return(newB(*

c++ - 我如何获得废弃的 boost::interprocess::interprocess_mutex 的所有权?

我的场景:一台服务器和一些客户端(虽然不多)。服务器一次只能响应一个客户端,因此他们必须排队。我正在使用互斥量(boost::interprocess::interprocess_mutex)来执行此操作,它封装在boost::interprocess::scoped_lock中。问题是,如果一个客户端在持有互斥量时意外死亡(即没有析构函数运行),其他客户端就会有麻烦,因为它们正在等待该互斥量。我考虑过使用定时等待,所以如果我的客户端等待20秒但没有获得互斥锁,它会继续与服务器通信。这种方法的问题:1)它每次都这样做。如果它处于循环中,不断与服务器对话,则每次都需要等待超时。2)如果有

c++ - std::unique_ptr 和 std::shared_ptr 之间破坏行为差异的基本原理是什么?

来自http://en.cppreference.com/w/cpp/memory/unique_ptr:IfTisderivedclass(sic)ofsomebaseB,thenstd::unique_ptrisimplicitlyconvertibletostd::unique_ptr.Thedefaultdeleteroftheresultingstd::unique_ptrwilluseoperatordeleteforB,leadingtoundefinedbehaviorunlessthedestructorofBisvirtual.Notethatstd::shared

c++ - 没有 RTTI 的 shared_ptr?

我正在尝试在使用xc321.34(gcc4.5.2的衍生物)构建的嵌入式项目中使用shared_ptr。该项目使用-fno-rtti禁用了RTTI。#include仅包含header会出现以下错误:/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory:Inmemberfunction'virtualvoid*std::tr1::_Ref_count_del::_Get_deleter(conststd::type_info

c++ - 使用 shared_ptr 和 weak_ptr 时避免间接循环引用

我目前正在组装一个严重依赖shared_ptr的应用程序,到目前为止一切看起来都很好-我已经完成了我的homework并且非常清楚使用shared_ptr的一些陷阱shared_ptr最常见的问题之一是循环依赖-这些问题可以通过存储weak_ptr来解决,这些weak_ptr不会影响上链对象的生命周期.但是,我很难理解需要通过weak_ptr存储指向外部对象的指针的时间-我不确定它是否被禁止、不鼓励,或者是否这是安全的。下图描述了我的意思(黑色箭头表示shared_ptr;虚线表示weak_ptr):alttexthttp://img694.imageshack.us/img694/6

c++ - 我可以将 boost::make_shared 与私有(private)构造函数一起使用吗?

考虑以下几点:classDirectoryIterator;namespacedetail{classFileDataProxy;classDirectoryIteratorImpl{friendclassDirectoryIterator;friendclassFileDataProxy;WIN32_FIND_DATAWcurrentData;HANDLEhFind;std::wstringroot;DirectoryIteratorImpl();explicitDirectoryIteratorImpl(conststd::wstring&pathSpec);voidincreme

c++ - 将 boost::shared_lock 升级为独占锁

谁能解释一下boost::upgrade_lock的正确用法。以下代码导致死锁//Globaltypedefboost::shared_mutexMutex;typedefboost::shared_lockReadLock;typedefboost::upgrade_lockUpgradeLock;typedefboost::upgrade_to_unique_lockWriteLock;MutexsharedMutex;//Multithreadedreaderandwriter{ReadLockread(sharedMutex);for(intii=0;ii如果我在升级前用rea

C++ 重载基于 shared_ptr 派生类的函数

有很多与此类似的SO问题,但我无法准确找到我要找的内容。如果这是重复的,我很抱歉。我有一个Parent类和继承自它的两个派生类:classSon:publicParent{...};classDaughter:publicParent{...};然后我声明两个shared_ptr到基类,并用shared_ptr实例化它们对于每个派生类:shared_ptrson=shared_ptr(newSon());shared_ptrdaughter=shared_ptr(newDaughter());最后,我想要一个处理shared_ptr的类基于它指向哪个派生类。问题是我可以使用函数重载来实

c++ - 为什么传递对 Mutex 类的引用不是一个好的设计?

来自这里:LogicerrorinmydefinedMutexclassandthewayIuseitinproducerconsumerprogram-pthreadsthewayyoupassaroundreferences(!)toyourmutexclassisplainlyaskingfortrouble,itdefiesanykindofencapsulation.为什么这是个问题?我应该按值传递然后编写复制构造函数吗?在这种情况下,缺乏封装会造成什么危害?我应该如何封装什么?此外,WhyispassingreferencestotheMutexclassnotagoodd

c++ - 我应该使用 shared_ptr<Object> myObject = (shared_ptr<Object>) new Object() 来访问私有(private)构造函数吗?

我正在使用广泛使用以下语法的代码库:shared_ptrmyObject=(shared_ptr)newObject();我注意到我无法使用make_shared访问私有(private)构造函数,但是shared_ptrmyObject=(shared_ptr)newObject();工作得很好。我应该仅仅因为它看起来有效而使用它吗?有什么危险吗?它与make_shared有何不同??我知道this中的答案问题,它在make_shared之间进行比较和:std::shared_ptrp2(newObject("foo"));但我没能找到对我遇到的语法的引用。和上面有什么不同,还是一样