草庐IT

shared_id

全部标签

c++ - 如何对 vector 进行二进制搜索以查找具有特定 id 的元素?

我有一个已排序的vector,现在我想从该vector中找到具有特定ID的元素。std::binary_search只是告诉我元素是否存在,所以我使用std::lower_bound:#include#include#includestructFoo{intid;//...moremembers...//Foo(intid):id(id){}};boolcompareById(constFoo&a,constFoo&b){returna.idvect;vect.push_back(10);vect.push_back(123);vect.push_back(0);std::sort(v

c++ - 使用 shared_ptr 时出现 SEGFAULT

我正在尝试使用shared_ptr在C++中实现LazyConcurrentList-basedSet。我的推理是unreachablenodes将被最后一个shared_ptr自动释放。根据我的理解,shared_ptr的引用计数的递增和递减操作是原子的。这意味着只有引用该节点的lastshared_ptr应该为该节点调用delete/free。我为多线程运行程序,但我的程序崩溃并出现错误doublefreecalled或只是SegmentationFault(SIGSEGV)。我不明白这怎么可能。下面给出了我的实现代码,方法名称表示它们的预期操作。#include#include#

c++ - shared_future<void> 是 condition_variable 的合法替代品吗?

Josuttis指出[“标准库”,第2版,第1003页]:Futuresallowyoutoblockuntildatabyanotherthreadisprovidedoranotherthreadisdone.However,afuturecanpassdatafromonethreadtoanotheronlyonce.Infact,afuture'smajorpurposeistodealwithreturnvaluesorexceptionsofthreads.另一方面,shared_future可以被多个线程使用,以识别另一个线程何时完成了它的工作。另外,一般来说,高级并发

c++ - react 线程需要其引用共享状态的 std::shared_future 的 OWN COPY

我对EffectiveModernC++的第270页有疑问,作者是ScottMeyers。第5/6行,他写道:“唯一的微妙之处在于每个react线程都需要引用共享状态的std::shared_future的自己的拷贝,...”我的问题是:为什么我们必须将std::shared_future的拷贝传递给每个线程中的每个lambda函数?而先验的,我没有看到通过引用传递它有任何问题,这样就有一个独特的共享状态可以被不同的线程使用?我写了一段改编自DrScottMeyers的书的代码,即使我通过了sfparreference,它仍然有效。因此,是否可以通过引用传递它?#include#inc

c++ - 从同一个指针构造两个 shared_ptr 对象

我有一个来自“C++标准库扩展”的问题:Exercise6IsaidinSection2.4.2thatyoushouldn'tconstructtwoshared_ptrobjectsfromthesamepointer.Thedangeristhatbothshared_ptrobjectsortheirprogenywilleventuallytrytodeletetheresource,andthatusuallyleadstotrouble.Infact,youcandothisifyou'recareful.It'snotparticularlyuseful,butwrit

c++ - std::make_shared 作为默认参数不编译

在VisualC++(2008和2010)中,以下代码无法编译并出现以下错误:#includevoidFoo(std::shared_ptrtest=::std::make_shared(5)){}classP{voidFoo(std::shared_ptrtest=::std::make_shared(5)){}};errorC2039:'make_shared':不是'`globalnamespace''的成员errorC3861:'make_shared':找不到标识符它提示P::Foo()而不是::Foo()的定义。有谁知道为什么Foo()有一个默认参数与std::make_s

c++ - 按值返回由互斥量保护的 shared_ptr 是否安全?

这是一个代码示例:classA{boost::mutexa_mutex;boost::shared_ptra;boost::shared_ptrclone_a(void){boost::lock_guardlock(a_mutex);returna;}};建议boost::shared_ptr对A::a的复制构造函数调用将在boost::lock_guard析构函数调用之前尽管有编译器优化。那么,调用A::clone_a()安全吗? 最佳答案 如果您所说的“安全”是指您不会在a上发生数据竞争,那么是的。正如你所说。但是,正如您可能知

c# - 从 C# 获取 CPU ID 代码到 C++

我有这个C#代码来获取处理器ID,但我无法将它传递给C++,我尝试了很多但我真的做不到,我刚开始使用C++,我希望能够获得C++的CPUID就像我以前用C#得到的一样这是我在C#中的代码:publicstaticstringGetProcessorID(){stringsProcessorID="";stringsQuery="SELECTProcessorIdFROMWin32_Processor";ManagementObjectSearcheroManagementObjectSearcher=newManagementObjectSearcher(sQuery);Managem

c++ - 我如何在 void* 和 boost shared_ptr 之间进行转换

我有这些行:typedefboost::shared_ptrA_SPtr;void*f(void*var){...我希望能够做这样的事情:A_SPtrinstance=(void*)(var);我该怎么做?另外,我怎样才能将意义从shared_ptr转换为void*? 最佳答案 只需将指针转换为指向和来自void*的共享指针。shared_ptr到void*:f(reinterpret_cast;(&A_SPtr));void*回到shared_ptr:A_SPtrinstance=*reinterpret_cast(boost::

c++ - 如何使用移动构造函数正确处理 shared_ptr 的映射?

假设我有一个容器std::map>我想用外部函数填充它并避免处理它的内容。所以我有typedefContainerstd::map>Container&&f(){Containerbar;autofoo=std::shared_ptr(newMyClass());bar.insert(std::make_pair(0,foo));std::coutprint_smthprint_smth如果我使用传统的复制构造函数,一切都会按预期工作。 最佳答案 这远太复杂了。为什么不直接说:intmain(){Containerbaz{{0,st