c ++读取时同步共享内存
c++ Synchronize shared memory when reading
我想同步一个 shared_memory_object
用于读取 iff
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 调用时崩溃。
如果要与另一个进程同步,请使用同步原语: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";
}
}
运行后台客户端数:
for a in {1..10}; do ./sotest& done
让他们都等着。启动服务器:
./sotest server
让他们都进步,他们表现:
Mapped the region of size 10240
我想同步一个 shared_memory_object
用于读取 iff
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 调用时崩溃。
如果要与另一个进程同步,请使用同步原语: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";
}
}
运行后台客户端数:
for a in {1..10}; do ./sotest& done
让他们都等着。启动服务器:
./sotest server
让他们都进步,他们表现:
Mapped the region of size 10240