进程间读取器/写入器锁定与加速

Interprocess reader/writer lock with Boost

本文关键字:锁定 加速 读取 进程      更新时间:2023-10-16

在解释如何使用 Boost 实现读取器/写入器锁定时,这个线程是黄金。它看起来相对简单,我真的很喜欢它,但它似乎也使用了一个非命名锁,我需要一个进程间解决方案(不需要是便携式的,可以仅限 Windows)。

有没有办法有一个进程间shared_mutex?我看到有一个named_mutex但我无法让它与其他锁一起使用shared_lock

任何指示都值得赞赏。

[编辑]

与此同时,我遇到了这个几乎一针见血的线程。我有两个问题:

  1. 它没有显示完整的代码(我猜我需要使用 named_upgradable_mutex但我不太确定)和
  2. 我不喜欢修改后的"编写器"的答案,它不使用在析构函数中解锁的现成类,而是在互斥锁上执行 3 个原始调用序列。

仍然欢迎评论或好的解决方案。

Boost.Interprocess 文档描述了它支持的所谓可升级互斥锁,以及两种受支持的可升级互斥锁类型的可升级互斥锁操作:

  • boost::interprocess::interprocess_upgradable_mutex,一种非递归的、匿名的可升级互斥锁,可以放在共享内存或内存映射文件中。
  • boost::interprocess::named_upgradable_mutex,一个非递归的,名为可升级互斥锁。

编辑:我相信这有效:

#include <iostream>
#include <string>
#include <unistd.h>
#include <boost/scope_exit.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/sharable_lock.hpp>
#include <boost/interprocess/sync/upgradable_lock.hpp>
// http://stackoverflow.com/questions/12439099/interprocess-reader-writer-lock-with-boost/
#define SHARED_MEMORY_NAME "SO12439099-MySharedMemory"
struct shared_data {
private:
    typedef boost::interprocess::interprocess_upgradable_mutex upgradable_mutex_type;
    mutable upgradable_mutex_type mutex;
    volatile int counter;
public:
    shared_data()
        : counter(0)
    {
    }
    int count() const {
        boost::interprocess::sharable_lock<upgradable_mutex_type> lock(mutex);
        return counter;
    }
    void set_counter(int counter) {
        boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mutex);
        this->counter = counter;
    }
};
int main(int argc, char *argv[])
{
    using namespace boost::interprocess;
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " WHICH" << std::endl;
        return 1;
    }
    const std::string which = argv[1];
    if (which == "parent") {
        shared_memory_object::remove(SHARED_MEMORY_NAME);
        shared_memory_object shm(create_only, SHARED_MEMORY_NAME, read_write);
        BOOST_SCOPE_EXIT(argc) {
            shared_memory_object::remove(SHARED_MEMORY_NAME);
        } BOOST_SCOPE_EXIT_END;
        shm.truncate(sizeof (shared_data));
        // Map the whole shared memory into this process.
        mapped_region region(shm, read_write);
        // Construct the shared_data.
        new (region.get_address()) shared_data;
        // Go to sleep for a minute.
        sleep(60);
        return 0;
    } else if (which == "reader_child") {
        shared_memory_object shm(open_only, SHARED_MEMORY_NAME, read_write);
        mapped_region region(shm, read_write);
        shared_data& d = *static_cast<shared_data *>(region.get_address());
        for (int i = 0; i < 100000; ++i) {
            std::cout << "reader_child: " << d.count() << std::endl;
        }
    } else if (which == "writer_child") {
        shared_memory_object shm(open_only, SHARED_MEMORY_NAME, read_write);
        mapped_region region(shm, read_write);
        shared_data& d = *static_cast<shared_data *>(region.get_address());
        for (int i = 0; i < 100000; ++i) {
            d.set_counter(i);
            std::cout << "writer_child: " << i << std::endl;
        }
    }
}

我使用以下脚本在Mac上尝试了此操作:

#!/usr/bin/env sh
./a.out reader_child &
./a.out reader_child &
./a.out writer_child &
./a.out reader_child &
./a.out reader_child &

(你必须先启动父级:./a.out parent

输出显示"reader_child"和"writer_child"行的交错(所有"reader_child"行在第一个"writer_child"行之后显示非零值),因此它似乎正在工作。