- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Boost Interprocess 库在两个进程之间共享内存。
我正在使用以下内容来分配共享内存块、附加 vector 、命名互斥量和命名条件变量:
using ShmemAllocator = allocator<T, managed_shared_memory::segment_manager>;
using MyVector = vector<T, ShmemAllocator>;
segment.reset(new managed_shared_memory(create_only, blockName, numBytes));
const ShmemAllocator alloc_inst(segment->get_segment_manager());
vec = segment->construct<MyVector>(sharedVectorName)(alloc_inst);
named_mutex.reset(new named_mutex(create_only, mutexName));
cond_empty.reset(new named_condition(create_only, conditionVName));
以及要删除的以下内容:
named_mutex::remove(mutexName);
named_condition::remove(conditionVName);
shared_memory_object::remove(blockName);
为了检查内存是否被正确删除,我进行了压力测试:
while(counter < 1000000)
{
MySharedMemoryObj s;
++counter;
}
(依赖RAII为析构函数删除共享内存)
我有三个问题:
我是否需要删除 vector ,因为它无论如何都是线段的一部分?
上面的方法有效,但在一个特定的情况下它没有,并抛出一个 Boost Interprocess 异常,说它没有访问内存的权限。是什么原因导致的/有没有办法避免它?
我注意到上面的代码似乎在/tmp 中生成了名为 outputXXXXXXXXXXX
的二进制文件。这些是什么?它们不会被删除,因此会累积。
最佳答案
- Do I need to delete the vector, because it's part of the segment anyway?
从技术上讲,这里并非如此(假设您也使用共享内存分配器)。但是,跳过销毁是一种不好的做法,尤其是当您的析构函数具有逻辑(不是微不足道)时。
- The above works, but on one particular occasion it didn't and threw a Boost Interprocess exception, saying it did not have permission to access the memory. What causes this/is there a way to avoid it?
确保程序在创建共享段时与预期用户一起运行。这就是赋予它文件级访问权限的原因。
例如如果您以 root
身份创建段,则您应该无法以其他用户身份打开它。
- I have noticed that the above code seems to generate binary files in /tmp named outputXXXXXXXXXXX. What are these? They aren't being deleted and therefore accumulate.
这没有多大意义。这些路径表明您使用的是 POSIX 系统。在 POSIX 上,shmem 通常存在于 /dev/shm
中,我认为不需要临时文件。
我建议临时文件可能是其他程序(比如您的 IDE?)的产物
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/sync/named_condition.hpp>
#include <vector>
namespace bip = boost::interprocess;
static auto blockName = "4f72909d-8265-4260-9bb1-6bd58f63812c";
static auto sharedVectorName = "54714711";
static auto mutexName = "b4eb63e0";
static auto conditionVName = "f7a95857";
template <typename T> using ShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>;
template <typename T> using MyVector = std::vector<T, ShmemAllocator<T> >;
int main() {
bip::managed_shared_memory segment(bip::create_only, "blockName", 10<<20u);
auto vec = segment.construct<MyVector<int> >(sharedVectorName)(segment.get_segment_manager());
bip::named_mutex named_mutex(bip::create_only, mutexName);
bip::named_condition named_condition(bip::create_only, conditionVName);
}
或者,根据您要同步的内容,使同步原语成为共享数据的成员:
struct SharedData {
using allocator_type = ShmemAllocator<int>;
template <typename A>
SharedData(A alloc) : _vec(alloc) {}
MyVector<int> _vec;
bip::interprocess_mutex _mx;
bip::interprocess_condition _cond;
};
int main(int argc, char**) {
bip::managed_shared_memory segment(bip::open_or_create, "2fc51845-3d9b-442b-88ee-f6fd1725e8b0", 10<<20u);
auto& data = *segment.find_or_construct<SharedData>("sharedData")(segment.get_segment_manager());
}
模拟最大容量为 10 个元素的队列。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <vector>
#include <mutex> // unique_lock
namespace bip = boost::interprocess;
template <typename T> using ShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>;
template <typename T> using MyVector = std::vector<T, ShmemAllocator<T> >;
struct SharedData {
using allocator_type = ShmemAllocator<int>;
template <typename A>
SharedData(A alloc) : _vec(alloc) {}
MyVector<int> _vec;
bip::interprocess_mutex _mx;
bip::interprocess_condition _cond;
using lock_type = std::unique_lock<bip::interprocess_mutex>;
lock_type wait_for_empty() {
lock_type lk(_mx);
_cond.wait(lk, [=] { return _vec.empty(); });
return lk;
}
void push(int v) {
lock_type lk(_mx);
_cond.wait(lk, [=] { return _vec.size() < 10; }); // wait for free space
_vec.push_back(v);
_cond.notify_all();
}
bool pop(boost::posix_time::time_duration timeout, int& out) {
lock_type lk(_mx);
// wait for message
auto deadline = boost::posix_time::microsec_clock::universal_time() + timeout;
if (_cond.timed_wait(lk, deadline, [=] { return !_vec.empty(); })) {
out = _vec.back();
_vec.pop_back();
_cond.notify_all();
return true;
}
return false;
}
};
int main(int argc, char**) {
bip::managed_shared_memory segment(bip::open_or_create, "2fc51845-3d9b-442b-88ee-f6fd1725e8b0", 10<<20u);
auto& data = *segment.find_or_construct<SharedData>("sharedData")(segment.get_segment_manager());
if (argc>1) {
// "server"
std::cout << "Waiting for queue to be depleted\n";
data.wait_for_empty();
for (int i = 0; i<20; ++i) {
std::cout << "Pushing " << i << "\n";
data.push(i);
}
} else {
// "client"
int what;
while (data.pop(boost::posix_time::seconds(1), what))
std::cout << "Popped " << what << "\n";
std::cout << "Timeout reached, bye\n";
}
}
关于c++ - Boost进程间共享内存删除、权限和输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50289336/
只是想知道是否有可能找出谁从 Windows 共享中读取了文件(最好使用 .NET,但 win32 native 可以)? 我想做的是创建类似 awstats 的东西对于 Windows 共享,这样我
是否可以列出 Intent.ACTION_SEND ?我的意思是我需要知道是否有人通过 action_send 在 Facebook 上分享或在 Twitter 上发推文。 最佳答案 也许你想要一个更
我正在使用 Google Apps 应用程序。实际上,我想在不使用密码的情况下访问另一个 ID。我使用了 OAuth,它运行良好。但我无法分享特定人的日历。我尝试了以下代码。 GoogleOAuthP
我怎样才能只创建模拟器...可能吗?我知道,设备需要分发证书。 最佳答案 您只需将应用程序目录从 iPhone 模拟器复制到另一个实例/操作系统版本,它就应该可以工作。 因此,如果您想分发 3.1.3
我想使用多阶段构建来避免每次构建应用程序时都下载我的 Java 项目所需的所有 Maven 依赖项。 我正在考虑在第一阶段解决 Maven 依赖项,然后在第二阶段构建应用程序,这将需要访问在前一阶段下
我正在寻找保护用户下载内容的初步想法。用户下载充满有趣资源的 zip 文件,这些资源被提取到本地文件系统中以供应用程序使用。我的目标是防止用户通过互联网将下载的资源共享给其他用户(假设他们获得了对文件
我想知道在具有移动和桌面版本的网站上共享身份验证、 session 管理等的最佳方法是什么。我们正在运行 Tomcat,并且更愿意将移动站点和桌面站点的应用程序保持在不同的节点上。 我看过类似的帖子,
我发现了这个单例的实现。我怎样才能创建指向它的指针或共享指针?` 为什么这不起作用?自动测试 = Singleton::Instance(); class Singleton { public: st
我有一个 heroku 项目,我想与其他人分享。作为the instructions describe ,我使用 virtualenv 来管理环境和依赖项。有没有办法在新机器上从 requiremen
Maven 将所有 jar 存储在本地存储库 ~/.m2/repository/ 下。用户多时占用空间大。 那么,是否可以由多个用户共享这个本地存储库,或许在不同的目录结构下? 最佳答案 简单的回答
为什么共享 worker 在重新加载页面时死了?应该是复活了我该如何解决这个问题? 重装前 重新加载后(在example.com上按F5) parent worker var port = new S
我正在开发多个小型应用程序,这些应用程序将共享通用和共享模块和 Assets 。 关于如何创建项目结构的部分在这里回答:https://stackoverflow.com/a/61254557/135
我在 RHEL 上安装了 jenkins (localhost:8080),我能够成功地构建代码 现在,我想设置主/从代理。 我的笔记本电脑将充当“Master Jenkins”,而我同事的笔记本电脑
我有这种方法可以根据我使用的 EXTRA_STREAM 共享文本文件或图片。我有这两个我可以选择 i.putExtra(Intent.EXTRA_STREAM, uri); i.putExtra(In
我正在使用 R 中的一个数据分析项目,我正在使用 R 中的敏感私有(private)数据进行一些逻辑和多级建模。我爱上了 。预订 包,我已经创建了一本关于我们的工作流程和分析管道的相当广泛的书。问题是
我正在构建的应用程序需要在 UITabBarController 框架内为多个 View (及其 subview )显示共享的自定义 UIToolbar。自定义工具栏的内容在所有 View 中都是相同
我有多个应用程序,我想共享相同的 eslint 配置: - project_root/ - app1/ - node_modules/ - eslint.rc
我有多个 Electron 应用程序。一个是主应用程序,其他几个功能应用程序。主应用程序上的按钮很少,这将导致功能应用程序打开。这里的问题是每个应用程序都有一个主进程,该进程导致要利用更多的CPU。是
我正在开发一个 Node.js 后端,它通过 websocket 与一些桌面客户端进行通信,而服务器端的通信是从 Web 前端发起的。一切正常,因为我将 SockJS Connection 实例存储在
我对托管多个网站的服务器上的多个用户帐户使用私有(private) SSH key 和无密码条目。 我为每个用户帐户使用相同的私钥。 (因为我很懒?或者那是“正确”的方式)。 我现在想授权该国不同地区
我是一名优秀的程序员,十分优秀!