考虑以下简单的引用计数函数(与 一起使用boost::intrusive_ptr):
class Foo {\n // ...\n\n std::atomic<std::size_t> refCount_{0};\n\n friend void intrusive_ptr_add_ref(Foo* ptr)\n {\n ++ptr->refCount_; // \xe2\x9d\xb6\n }\n\n friend void intrusive_ptr_release(Foo* ptr)\n {\n if (--ptr->refCount_ == 0) { // \xe2\x9d\xb7\n delete ptr;\n }\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n\n我仍在学习内存排序,我想知道在这种情况下fetch_add/sub( memory_order_seq_cst) 的默认内存排序是否太严格。由于我想确保的唯一顺序是在 \xe2\x9d\xb6 和 \xe2\x9d\xb7 之间,我认为我们可以将 \xe2\x9d\xb6 替换为
ptr->refCount_.fetch_add(1, std::memory_order_release);\nRun Code Online (Sandbox Code Playgroud)\n\n和 \xe2\x9d\xb7 与
\n\nif (ptr->refCount_.fetch_sub(1, std::memory_order_consume) == 1) {\nRun Code Online (Sandbox Code Playgroud)\n\n但内存排序对我来说仍然是新的和微妙的,所以我不确定这是否能正常工作。我错过了什么吗?
\nc++ multithreading reference-counting memory-model stdatomic
假设我有一个名为的共享库libplugin.在这个共享库中,有一个类:
class Plugin
{
public:
virtual void doStuff();
};
Run Code Online (Sandbox Code Playgroud)
我们还假设有另一个名为的共享库libspecialplugin.它包含以下类和函数:
class SpecialPlugin : public Plugin
{
public:
virtual void doStuff();
};
Plugin *createSpecialPlugin()
{
return new SpecialPlugin;
}
Run Code Online (Sandbox Code Playgroud)
现在,假设我更改Plugin并添加以下方法:
virtual void doMoreStuff();
Run Code Online (Sandbox Code Playgroud)
我不重新编译libspecialplugin.
当我这样做时会发生什么:
Plugin *plugin = createSpecialPlugin();
plugin->doMoreStuff();
Run Code Online (Sandbox Code Playgroud)
我猜是发生以下其中一种情况:
Plugin::doMoreStuff()调用该方法该libspecialplugin库是否包含libplugin可用于确定哪些方法被覆盖的信息 - 即使在运行时也是如此?我对这应该发生的事情有点模糊.
以下代码:
class A1 {
public:
int x;
};
class A2 {
private:
int x() { return 67; }
};
class M : public A1, public A2 {};
int main() {
M m;
m.x;
}
Run Code Online (Sandbox Code Playgroud)
编译出错:
error C2385: ambiguous access of 'x'
note: could be the 'x' in base 'A1'
note: or could be the 'x' in base 'A2'
Run Code Online (Sandbox Code Playgroud)
但为什么?只A1::x应该对 M 可见。
A2::x应该是纯本地的。
c++ access-control multiple-inheritance private-members name-lookup
我的问题是这个说法:
如果任何std :: weak_ptr在所有共享所有者的生命周期结束后引用由std :: make_shared创建的控制块,则T占用的内存将持续存在,直到所有弱所有者都被销毁,如果sizeof(T)是大.资源
我在这里读到,这个对象一直存在,直到最后一个weak_ptr存在.它是使用make_shared自由对象,循环引用self还是永远存在于内存中?
例如:
struct A
{
std::weak_ptr<A> parent;
}
void fn()
{
auto a=std::make_shared<A>();
a->parent = a;
} // Will it destroy here or not?
Run Code Online (Sandbox Code Playgroud) 以下代码无法编译:
#include <iostream>
#include <memory>
class A
{
public:
A( )
: m_i( new int )
{ }
std::shared_ptr< const int >&
get( )
{
return m_i; // <-- invalid initialization of reference of type
// 'std::shared_ptr<const int>&' from
// expression of type 'std::shared_ptr<int>'
}
private:
std::shared_ptr< int > m_i;
};
int main( )
{
A a;
auto& i = a.get( );
std::cout << *i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何从共享指针转换为常量对象的共享指针?static_cast也失败了.
编译器不会为以下代码抛出任何警告或错误.const限定符的含义是否被滥用?显然,我不能在同一循环迭代中重新分配它,但它确实似乎在每次迭代后重新分配它.
示例代码:
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#pragma pack(push, 4)
class C {
public:
char a;
char b;
short c;
char d;
};
C e;
Run Code Online (Sandbox Code Playgroud)
我不明白为什么sizeof(e)是6.我认为sizeof(e)将是8 => 1(a)+ 1(b)+ 2(c)+ 1(d)+ 3(对齐)
我有一个派生类,我希望其中一个函数覆盖其在基类中的版本,但具有不同的签名。简单的例子:
#include "stdio.h"
bool use_foo = false;
class Foo {
public:
virtual int func(double x) { printf ("%f in Foo!\n", x); }
};
class Bar : public Foo {
public:
int func(short x) { printf ("%d in Bar!\n", x); }
};
int main () {
Foo* A;
if (use_foo)
A = new Foo;
else
A = new Bar;
A->func(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使 A 被分配为派生类,上面的代码也会调用基类副本:
> g++ test.cpp -o test -O3 && ./test
2.000000 in Foo!
Run Code Online (Sandbox Code Playgroud)
因为(据我的理解)参数可以转换为匹配基类签名,并且派生类不会因为这种差异而覆盖它(但在这种情况下它不会隐藏它吗?)。如果我将基类函数更改short …
cppreference.com提供以下有关std::atomic_thread_fence(强调我的)注释:
atomic_thread_fence比具有相同std :: memory_order的原子存储操作施加更强的同步约束。
虽然原子存储释放操作可以防止所有先前的写操作移出存储释放,但是具有memory_order_release排序的atomic_thread_fence可以防止所有先前的写操作移过所有后续存储。
我了解此注释的意思 std::atomic_thread_fence(std::memory_order_release)不是像存储版本那样单向的。这是双向栅栏,可防止栅栏两侧的商店重新排序,使其越过栅栏另一侧的商店。
如果我正确理解这一点,那么这道篱笆似乎可以提供同样的保证atomic_thread_fence(memory_order_acq_rel)。它是“向上”的栅栏,是“向下”的栅栏。
有没有之间的功能差异std::atomic_thread_fence(std::memory_order_release)和std::atomic_thread_fence(std::memory_order_acq_rel)?还是为了记录代码的目的,差异仅仅是美观?
以下是cppreference的定义:
[ https://en.cppreference.com/w/cpp/named_req/CopyConstructible][1]
要求:类型T满足CopyConstructible if
类型T满足MoveConstructible和Given
v,类型为T或const T的左值表达式或类型为const T u的rvalue表达式,任意标识符以下表达式必须有效且具有指定的效果
表达式后置条件T u = v; u的值等于v的值.v的值不变
T(v)T(v)的值等于v的值.v的值不变.
我的问题是以下几行:
v,类型为T或const T的左值表达式或类型为const T的右值表达式
我不理解这个"const T型的rvalue表达式"的一部分?为什么这个"类型为const T的rvalue表达式"在CopyConstructible的定义中是什么?有人可以用例子解释一下吗?MoveConstructible的定义(这是CopyConstructible的先决条件)已经包含了这个要求,不是吗?
c++ ×10
const ×3
memory-model ×2
shared-ptr ×2
atomic ×1
c ×1
c++11 ×1
lifetime ×1
make-shared ×1
name-lookup ×1
overriding ×1
pragma ×1
return-value ×1
sizeof ×1
stdatomic ×1
vtable ×1
weak-ptr ×1