草庐IT

atomic_shared_ptr

全部标签

c++ - 在一组 shared_ptr 中找到一个值

我有一组shared_ptr并想在其中找到一个值:typedefstd::shared_ptrIntPtr;structCompare{booloperator()(constIntPtr&a,constIntPtr&b){return*as;autox=std::make_shared(3);s.insert(x);boolfound=s.find(std::make_shared(3))!=s.end();它可以工作,但效率不高-每次尝试查找值时都需要新的临时指针。还有其他办法吗?看起来像Searchinginasetofshared_ptr有一些可能有用的想法吗?

c++ - 如果已经被互斥锁保护,我是否需要使用 atomic<>

给定thispost中的代码,实现Semaphore仅使用atomic和mutex.我很好奇自count已经被updateMutex守护,是atomic有必要吗?structSemaphore{intsize;atomiccount;mutexupdateMutex;Semaphore(intn):size(n){count.store(0);}voidaquire(){while(1){while(count>=size){}updateMutex.lock();if(count>=size){updateMutex.unlock();continue;}++count;update

c++ - 如何打印 std::atomic<unsigned int> 的值?

我正在使用std::atomic在我的程序中。如何使用printf打印它的值?如果我只使用%u是行不通的.我知道我可以使用std::cout,但我的程序中到处都是printf电话,我不想更换他们中的每一个。以前我用的是unsignedint而不是std::atomic,所以我只是使用%u在我的printf中的格式字符串中调用,因此打印工作正常。尝试打印std::atomic时遇到的错误现在代替常规unsignedint是:error:format‘%u’expectsargumentoftype‘unsignedint’,butargument2hastype‘std::atomic’

c++ - "error: use of deleted function"在 move 构造函数中对 unique_ptr 调用 std::move 时

#includeclassA{public:A(){}A(constA&&rhs){a=std::move(rhs.a);}private:std::unique_ptra;};此代码无法使用g++4.8.4编译并抛出以下错误:error:useofdeletedfunction‘std::unique_ptr&std::unique_ptr::operator=(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delete]’a=std::move(rhs.a);^我知道unique_ptr的复制构造函数和复制赋值构造函数已删除

c++ - 在同一个线程的同一个实例上多次调用 shared_future::get() 是否合法?

我找不到关于此事的直接确认或反驳。所有答案似乎都解决了“从多线程访问”方面的问题,而不是重复访问本身。标准是否定义了std::shared_future的行为?boost::shared_future怎么样? 最佳答案 根据std::shared_future::valid中的cppreferenceUnlikestd::future,std::shared_future'ssharedstateisnotinvalidatedwhenget()iscalled.这是有道理的。如果不是这种情况,那么您将无法让多个线程调用get。.我

c++ - 如何解决 boost::shared_ptr 和使用 std::shared_ptr 之间的冲突?

如果我在此代码段中从boost::shared_ptr更改为std::shared_ptr,我将收到链接器错误。#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include//usingnamespacestd;//usingnamespaceboost;usingstd::string;usingstd::ostringstre

c++ - 从函数返回的 unique_ptr 的范围是什么?

这能正常工作吗?(见示例)unique_ptrsource(){returnunique_ptr(newA);}voiddoSomething(A&a){//...}voidtest(){doSomething(*source().get());//unsafe?//Whendoesthereturnedunique_ptrgooutofscope?} 最佳答案 从函数返回的unique_ptr没有范围,因为范围只适用于名称。在您的示例中,临时unique_ptr的生命周期以分号结束。(所以是的,它会正常工作。)一般来说,当完整表达

c++ - 为什么 make_unique 不能与 unique_ptr::reset 一起使用?

我尝试用VS2013编译一些C++代码,unique_ptr::reset()似乎不适用于make_unique();一个小的可编译重现代码片段如下:#includeusingnamespacestd;intmain(){unique_ptrp=make_unique(3);p.reset(make_unique(10));}从命令行编译:C:\Temp\CppTests>cl/EHsc/W4/nologotest.cpp这些是来自MSVC编译器的错误:test.cpp(6):errorC2280:'voidstd::unique_ptr>::reset>>(_Ptr2)':attem

c++ - "atomic"和 "cstdatomic"有什么区别?

有人可以澄清一下包含选项之间的区别吗#include和#inlucde?我猜没有,因为它的行为相同?我问这个是因为在我的debian系统上我只有atomic而在我的kubuntu系统上我有cstdatomic。Debian上的编译器:版本4.7.2(Debian4.7.2-4)Kubuntu上的编译器:版本4.6.3(Ubuntu/Linaro4.6.3-1ubuntu5) 最佳答案 现有的两个答案都是错误的,大多数评论也是如此。不是任何标准中定义的header。它在旧的C++0x草案中定义,但不在最终的C++11标准中,仅是。因此

c++ - 如何在必须复制构造的类中使用 std::auto_ptr?

我的foo类包含一个std::auto_ptr成员,我想复制它的构造,但这似乎是不允许的。作业也有类似的事情。请参阅以下示例:structfoo{private:int_a;std::string_b;std::auto_ptr_c;public:foo(constfoo&rhs):_a(rhs._a),_b(rhs._b),_c(rhs._c)//error:Cannotmutaterhs._ctogiveupownership-D'Oh!{}foo&operator=(constfoo&rhs){_a=rhs._a;_b=rhs._b;_c=rhs._c;//error:Samep