【问题标题】:Converting from std::string to "MyShmString" when sharing memory with boost::interprocess与 boost::interprocess 共享内存时从 std::string 转换为 "MyShmString"
【发布时间】:2018-04-11 07:18:26
【问题描述】:

我正在关注boost::interprocess guideStack Overflow thread,以便在不同进程之间共享 std::string 变量。具体来说,我有一个函数可以接收一个应该在其他进程之间共享的 std::string:

void registerFailedTests(std::string failed_test_name) {

    boost::interprocess::permissions perm;
    perm.set_unrestricted();

    boost::interprocess::shared_memory_object::remove(unit_tests_constants::shm_failed_tests_list.c_str());
    boost::interprocess::managed_shared_memory managed_shm_failed_tests_list(boost::interprocess::open_or_create, unit_tests_constants::shm_failed_tests_list.c_str(), 10240, 0, perm);

    typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
    typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> myShmString;

    CharAllocator charallocator(managed_shm_failed_tests_list.get_segment_manager());

    myShmString mystring(charallocator);
}

问题是参数 failed_test_name 是一个 std::string,我不能把它变成 myShmString 类型以便在共享内存中分配它。

我怎样才能做到这一点?

非常感谢。

【问题讨论】:

    标签: c++ boost shared-memory interprocess


    【解决方案1】:

    只需使用从输入序列复制的任何构造函数:

    myShmString mystring(failed_test_name.begin(), failed_test_name.end(), charallocator);
    

    【讨论】:

    • 非常感谢!这就是我需要的!我以前看过 basic_string 构造函数,但我无法将该信息与我的问题绑定。我真的很感谢你!
    • 干杯!欢迎来到 SO。另见meta.stackexchange.com/questions/5234/…
    猜你喜欢
    • 2020-10-19
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    相关资源
    最近更新 更多