C ++读取时同步共享内存 [英] c++ Synchronize shared memory when reading

查看:472
本文介绍了C ++读取时同步共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要同步shared_memory_object以读取iffshared_memory_object已存在.这是我用于与bool变量同步的代码.

I want to synchronize a shared_memory_object for reading iff the shared_memory_object already exists. This is the code I am using for synchronization with a bool variable.

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);

如果存在共享内存,那么我没有任何问题,可以在进程之间共享数据,但是,如果不存在共享内存,则程序将在mapping_region调用时崩溃.

If the shared memory exists, I do not have any problems, the data is shared among the processes, but if the shared memory is not present the program crashes at the mapped_region call.

推荐答案

如果要与另一个进程同步,请使用同步原语:

If you want to synchronize with another process, use a synchronization primitive: condition variable with a mutex:

#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";
    }
}

在后台运行许多客户端:

Running a number of clients in the background:

for a in {1..10}; do ./sotest& done

让他们全部等待.启动服务器:

Makes them all wait. Starting a server:

./sotest server

使他们取得所有进步,并显示:

Makes them all progress, and they show:

Mapped the region of size 10240

这篇关于C ++读取时同步共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆