使用C++读取共享内存时如何同步?

4

我希望能在shared_memory_object已经存在的情况下,为读取同步。这是我使用bool变量进行同步的代码。

boost::interprocess::shared_memory_object my_shared_mat;
bool mat_ready = true;
while (mat_ready)
{
    try {
        my_shared_mat = boost::interprocess::shared_memory_object(
            boost::interprocess::open_only,  // only open
            "shared_mat",                    // name
            boost::interprocess::read_only); // read-only mode
        mat_ready = false;
    }
    catch (boost::interprocess::interprocess_exception &ex) {
        std::cout << ex.what() << std::endl;
        mat_ready = true;
    }
}

boost::interprocess::mapped_region region(my_shared_mat, boost::interprocess::read_only);

如果共享内存存在,我就没有任何问题,数据会在进程之间共享。但是如果共享内存不存在,在调用mapped_region时程序就会崩溃。


你展示的代码不可能在对象不存在的情况下调用mapped_region函数。我建议你展示真实的代码。 - Jonathan Potter
我正在另一个进程中创建该对象。我想知道如果进程尚未创建,如何为此文件创建用于读取的共享内存。如果共享内存未被创建,它将抛出异常,因此我使用 while(mat_ready) 来解决这个问题。但是,如果这样做,在调用 mapped_region 时会导致崩溃。 - Luis
@JonathanPotter 如果服务器仍在创建映射区域(调整大小等),则存在竞争条件。是的,代码没有显示它,但也没有显示同步。 - sehe
1个回答

4
如果您想与另一个进程同步,请使用同步原语:条件变量互斥量
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/named_condition.hpp>
#include <iostream>

namespace bip   = boost::interprocess;
using Mutex     = bip::named_mutex;
using Condition = bip::named_condition;

int main(int argc, char**) {
    Mutex     mx(bip::open_or_create, "shared_mat_mx");
    Condition cv(bip::open_or_create, "shared_mat_cv");

    if (argc>1) { // server
        auto mat = bip::shared_memory_object(bip::create_only, "shared_mat", bip::read_write);
        mat.truncate(10 << 10); // 10kb

        bip::mapped_region region(mat, bip::read_only);

        {
            bip::scoped_lock<Mutex> lk(mx);
            cv.notify_all(); // notify all clients we're there
        }
    } else {
        {
            bip::scoped_lock<Mutex> lk(mx);
            cv.wait(lk); // wait for server signal
        }
        auto mat = bip::shared_memory_object(bip::open_only, "shared_mat", bip::read_only);
        bip::mapped_region region(mat, bip::read_only);

        std::cout << "Mapped the region of size " << region.get_size() << "\n";
    }
}

在后台运行多个客户端:
for a in {1..10}; do ./sotest& done

使他们等待。启动服务器:
./sotest server

使它们都进步,它们展现:
Mapped the region of size 10240

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接