【发布时间】:2018-08-15 12:31:20
【问题描述】:
我在 github repository 上有代码示例,并在 travis-ci 上创建了一个构建以便于复制。
最小、完整和可验证的示例
可能不是最小,但我相信它足够小
它使用 boost.interprocess 库 (boost::interprocess::managed_shared_memory) 创建一个共享内存区域,然后使用来自 boost 库的该区域分配器创建一个常规 STL unordered_map。
代码是我当前封闭源代码库中的精简版本,灵感来自sehe 的问题std::unordered_map with boost::interprocess allocator in shared memory - drawbacks? 中的答案
#include <sys/mman.h>
#include <sys/syscall.h>
#include <functional>
#include <memory>
#include <unordered_map>
#include <string>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
class Thing {
public:
volatile Thing *_parent;
explicit Thing(Thing *parent) : _parent(parent) {}
};
namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K, typename V, typename KH = std::hash<K>, typename KEq = std::equal_to<K>>
using HashMap = std::unordered_map<K, V, KH, KEq, Alloc<void>>;
typedef HashMap<pid_t, Thing> ThingMap;
int main() {
boost::interprocess::shared_memory_object::remove("test");
Segment my_segment{ipc::create_only, "test", 1ul<<40};
Manager *my_manager = my_segment.get_segment_manager();
ThingMap *my_map = my_segment.find_or_construct<ThingMap>("my_map")(my_manager);
my_map->emplace(123, nullptr);
printf("Hello world\n");
return 0;
}
问题
1。 clang++ 需要安装g++?
使用 Ubuntu 14.04,如果我安装 clang++-6.0 或 clang++-5.0 而不更新 g++(默认为 4.9 版),我最终会出现编译错误。
这与未安装 libc++ 和 clang 默认情况下未安装 c++ 库并诉诸使用系统中的内容有关 - 与 g++-4.9 捆绑在一起的那个?
2。我的代码需要 GNU 扩展吗?
显然,如果我指定-std=c++17,它将失败并显示g++-8。但是,g++-7 和 g++-6 会成功。
- g++-8 with extensions succeeds VS without extensions fails
- g++-7 with extensions succeeds VS without extensions succeeds
- g++-6 with extensions succeeds VS without extensions succeeds
由于我在 clang 构建中安装 g++-8,它们也失败了。我的猜测是,如果我使用g++-7,他们就会成功。 Build details with -std=c++17
非常感谢任何最佳实践建议。这是我在travis-ci 或clang 中使用clang 的少数第一次尝试。
【问题讨论】:
标签: c++ gcc boost clang travis-ci