草庐IT

false-sharing

全部标签

c++ - shared_ptrs 的 vector 行为神秘

我创建了一个Base类shared_ptr的vector来保存Derived类shared_ptr,并运行到一些问题。下面的简化示例显示了发生的情况。#include#include#includeusingnamespacestd;classBase{public:Base(inti):val(i){}intval;};classDerived:publicBase{public:Derived(inti):Base(i){}};intmain(){vector>vec1{make_shared(5),make_shared(99),make_shared(18)};for(auto

c++ - 为什么 std::shared_ptr 的别名构造函数不初始化 std::enabled_shared_from_this?

考虑以下代码:structFoo:std::enable_shared_from_this{};structBar{Foofoo;};intmain(){std::shared_ptrbar_p(newBar);//makeshared_ptrtomemberwithaliasingconstructorstd::shared_ptrfoo_p(bar_p,&bar_p->foo);assert(bar_p->foo.shared_from_this());//fail!throwsbad_weak_ptr}不幸的是,它没有按预期工作(至少在GCC4.8.2中)。我查看了代码,似乎别名

c++ - 对象 std::shared_ptr 是否可以通过它的 std::weak_ptr 找到?

所以我有一个std::vector>myListOfT;我有一个std::weak_ptrptrToOneT;它是从用于填充该容器的指针之一创建的(假设我将它放在回调函数中)。威尔std::find在那个容器和我的weak_ptr给我一个原始shared_ptr的迭代器(如果集合中存在这样的一个)?它是在标准的某处得到保证还是依赖于此实现? 最佳答案 通过使用std::weak_ptr::owner_before,我们可以不锁定weak_ptr。我将使用比必要的稍微冗长的解决方案并引入owner_equal,它与std::owner

c++ - boost::proto::is_aggregate 为聚合类型时返回 false

在测试聚合类型时,我尝试使用boost::proto::is_aggregate来检查我创建的类型是否真正聚合。我写了这段代码:#include#includestructIsAggregate{IsAggregate&operator=(IsAggregateconst&rhs){}};intmain(){std::cout()我希望输出为真,因为聚合类型可以定义复制赋值运算符(根据此:WhatareAggregatesandPODsandhow/whyaretheyspecial?)但是输出是错误的。我还在之前的答案中使用了聚合类,它应该返回true却返回了false。这已在Boo

c++ - boost::shared_ptr<string> 标准集

我有一组boost::shared_ptr,我希望它不是通过共享指针而是通过字符串来排序和唯一化的。我是否必须提供一个新的比较函数来获取共享指针并比较内容,或者已经存在我可以使用的比较器? 最佳答案 这非常具体,因此您可能需要一个自定义比较器。这应该有效:structpointercompare{booloperator()(constboost::shared_ptr&a,constboost::shared_ptr&b){return(*a)>(*b);}} 关于c++-boost:

c++ - 将 std::shared_ptr<char> 转换为 std::shared_ptr<unsigned char>

有什么好的方法可以将shared_ptr转换为shared_ptr吗?我想到了以下但它看起来不是很干净。intmain(intargc,char**argv){std::shared_ptrp1=std::make_shared();std::shared_ptrp2=std::shared_ptr(reinterpret_cast(p1.get()),[p1](unsignedchar*){});} 最佳答案 你正在做的事情有一个现成的功能,reinterpret_pointer_cast:std::shared_ptrp2=st

c++ - 这个 shared_ptr 是如何自动转换为原始指针的?

我现在正在学习C++11的enable_shared_from_this;一个例子让我感到困惑:shared_from_this()返回的shared_ptr类型如何转换为这个原始指针?#include#include#includestructBar{Bar(inta):a(a){}inta;};structFoo:publicstd::enable_shared_from_this{Foo(){std::coutgetBar(inta){std::shared_ptrpb(newBar{a},std::bind(&Foo::showInfo,shared_from_this(),s

日志报错 git -c dif.mnemonicprefix=false -c core.guotepath=false 解决方法

        前言:            在进行下面操作前,必须确保,你是否安装了Git。           查看Git                在命令行窗口中输入`git--version`:                如果这个命令成功显示了Git的版本信息,这表明Git已经被安装。            1.使用Sourcetree        SourceTree是Windows和MacOSX下免费的Git和Hg客户端,拥有可视化界面,容易上手操作。同时它也是Mercurial和Subversion版本控制系统工具。支持创建、提交、clone、push、pull和mer

c++ - GCC 发出用于 boost::shared_ptr 取消引用的额外代码

我有以下代码:#includestructFoo{inta;};staticintA;voidfunc_shared(constboost::shared_ptr&foo){A=foo->a;}voidfunc_raw(Foo*constfoo){A=foo->a;}我以为编译器会创建相同的代码,但对于shared_ptr版本,发出了一条额外的看似冗余的指令。Disassemblyofsection.text:00000000:0:55pushebp1:89e5movebp,esp3:8b4508moveax,DWORDPTR[ebp+8]6:5dpopebp7:8b00moveax,

c++ - 无法分配 tr1::shared_ptr

我有一个以tr1::shared_ptr作为成员的类,如下所示:classFoo{std::tr1::shared_ptrbsp;voidBar();}在成员函数Bar中,我尝试这样赋值:bsp=newTCODBsp(x,y,w,h);g++然后给我这个错误nomatchfor‘operator=’in‘((yarl::mapGen::MapGenerator*)this)->yarl::mapGen::MapGenerator::bsp=(operatornew(40u),(,((TCODBsp*))))’/usr/include/c++/4.4/tr1/shared_ptr.h:8