Acquire/Release Operation 和 Acquire/Release Fence
其实是 Jeff Preshing 的 Acquire and Release Fences Don’t Work the Way You’d Expect 这篇文章的笔记。
其实是 Jeff Preshing 的 Acquire and Release Fences Don’t Work the Way You’d Expect 这篇文章的笔记。
拿 learn-asio 这个项目练手时发现一个问题:learn-asio 依赖了 KBase,这两个项目都是我用 anvil 进行托管的,所以两个项目的 cmake 目录里各自有一份 compiler_*.cmake
。
因为 compiler_*.cmake
提供的函数 apply_common_compiler_property_to_target()
默认使用了非常严格的属性,导致构建 asio 练手工程出现很多 warning 和静态分析的错误,所以为了省事我把 learn-asio 的这个函数做了宽松化处理。
考虑到 ASIO 的相当一部分组件(subset)已经进入了 C++ 20 Networking TS,未来再经过一些小调整之后成为标准库的一员基本板上钉钉。
那么提前熟悉一下 ASIO 也不是什么坏事,毕竟没准等到 C++ 23 的时候,coroutine, networking, executor 啥的都已经很完备了,谁都可以用 C++ 实现一些性能不差的 server-end 代码了(比如 RPC 框架?)。
不过和使用 Boost 类似,使用 ASIO 作为依赖库也不是一件容易的事情;毕竟自 modern C++ 元年至今都8年了,CMake 还在成为 de facto building system 的道路上挣扎,广泛接受的包管理依然不见踪影…
陈年老账就不再扯了,得益于 CMake 3.2 开始提倡的 taget-based properties 和 CMake 3.11 开始提供的 FetchContent
module,我花了一些时间实现了一个基于 FetchContent
的 asio-cmake wrapper。
最近学习 MySQL 的过程中发现一些很坑的点,总结记录如下:
C++ 的 std::unique_ptr
有个神奇的特性:如果使用默认的 deleter(即使用 operator delete
),或者 non-capturing lambda 作为 deleter,则有
1 | sizeof(std::unique<T>) == sizeof(void*); |
即整个对象的内存布局和 trivial pointer 一致,没有额外的开销。
这个特性的背后就是 compress-pair;这个设施能够在某个元素是一个 empty class 时避免为其分配内存。
注:这里假设你知道什么是 EBO,以及为什么会有 EBO。
这里自己动手实现一个 compressed pair:
1 | template<typename Tx, typename Ty, bool = std::is_empty<Tx>::value> |
因为 EBO 是实现的核心,而父类的构造顺序先于子类的任何成员,上面将 Tx
作为可被优化的成员。