标签: boost-interprocess

各种版本的boost之间的boost :: interprocess :: managed_shared_memory的兼容性

是否有任何明确的兼容性保证boost :: interprocess :: managed_shared_memory可以在不同的boost版本中工作?我打算用它在多个进程之间共享一个整数或十个整数(这实际上将充当它们所有读取和写入的数据的修订号).这些过程是单独发布的,并且偶尔会终止.

问题是:我是否因为1.51中的managed_shared_memory无法与1.44中的managed_shared_memory交谈等等而将自己锁定到永久性的提升版本?

c++ boost boost-interprocess

9
推荐指数
1
解决办法
237
查看次数

如何调试或修复涉及boost :: interprocess managed_shared_memory的无限循环和堆损坏问题?

我有以下"第一次机会异常"消息,它来自我写的DLL,它运行在我没写的可执行文件中.也就是说,DLL是一个插件.第一次触发此异常时,尝试打开共享内存映射文件失败.如果我忽略了第一次机会异常并且只是运行,应用程序最终会冻结或崩溃.

First-chance exception at 0x76a7c41f in notmyexe.exe: Microsoft C++ exception: boost::interprocess::interprocess_exception at memory location 0x002bc644..
Run Code Online (Sandbox Code Playgroud)

几个小时之后,它似乎是由一个代码块引起的,这个代码块无休止地循环,直到预期的异常条件清除为止.事实证明,如果它永远不会清除,那么,最终,这个异常会变成另一个低级异常条件和/或变成堆损坏.所有这些只是为了使用Boost :: interprocess打开共享内存区域.

使事情变得复杂的第一件事是,在我的基于Visual C++ 2008的项目中,第boost::interprocess::interprocess_exception一次机会异常没有被抛出并被识别为它来自的位置,因为Visual C++ 2008编译器无法找到复杂的boost-flavor模板代码.题.但是,通过单步执行汇编语言视图,我发现代码爆炸了.

我自己的代码的顶级行开始变坏了:

  segment = new managed_shared_memory(   open_or_create
                                      ,  MEMORY_AREA_NAME
                                      , SHARED_AREA_SIZE );          
Run Code Online (Sandbox Code Playgroud)

上面的managed_shared_memory类来自interprocess_fwd.hpp,是boost共享内存API /头文件的标准部分.因为它是基于模板的,所以上面扩展到大约2Kchars长的C++ boost模板表达式,它由链接器和调试器截断不同的长度.Visual C++ 2008没有更多源代码调试功能,似乎在这些限制发挥作用时.

例如,当它爆炸时,我得到这个调用堆栈:

    KernelBase.dll!76a7c41f()   
    [Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]    
    KernelBase.dll!76a7c41f()   
>   msvcr90d.dll!_malloc_dbg(unsigned int nSize=2290875461, int nBlockUse=264, const char * szFileName=0x01fcb983, int nLine=1962999808)  Line 160 + 0x1b bytes C++
    8bfc4d89()  
Run Code Online (Sandbox Code Playgroud)

上面的堆栈转储中没有实际的最终用户编写的源函数.

我该怎么调试呢?其次,使用Visual C++ …

c++ boost boost-interprocess visual-c++-2008

9
推荐指数
1
解决办法
2038
查看次数

如何在c ++中使用boost实现与信号量类似的东西?

我注意到提升似乎不支持信号量.获得类似效果的最简单方法是什么?

c++ multithreading boost boost-thread boost-interprocess

8
推荐指数
2
解决办法
2万
查看次数

在mysql中同步存储过程执行

我在mysql中有一个存储过程,用于执行需要同步的任务,这样如果两个应用程序调用存储过程,只有一个可以访问一段代码来执行任务,保持另一个被阻塞直到第一个一个完成任务.

DELIMITER $$
CREATE PROCEDURE SP_GEN_ID(IN NAME VARCHAR(20))

BEGIN 
  DECLARE maxLen int default 0;
START TRANSACTION;
   #the section of code that needs to be synchronized
COMMIT
END;
$$

DELIMITER ;
Run Code Online (Sandbox Code Playgroud)

因此,如果两个应用程序同时调用存储过程,则必须同步任务.

一个.但是启动TRANSACTIONCOMMIT没有同步执行.

湾 并且LOCK TABLES tableA不能用于存储过程以确保同步.

C.我试图在应用程序级别同步存储过程调用.我用了

boost_interprocess scoped_lock lock();

它在1.41升级中运行得非常好

但是boost 1.34库不支持进程间锁定互斥锁,这是我的情况.

有没有办法同步代码的存储过程部分,以便在同时进行两次调用时,一个在另一个执行之前被阻塞?

(添加以下内容)编辑的代码:了解我在存储过程的synchronized块中尝试执行的操作.

它获取最后分配的id,并将其递增1并检查它是否未用于其他"名称"记录.找到有效的id后,更新最后分配的id记录表,然后将其与给定的"name"相关联.

DELIMITER $$
CREATE PROCEDURE SP_GEN_ID(IN NAME VARCHAR(20))

BEGIN 
  DECLARE maxLen int default 0;
START TRANSACTION;
   #the section of code that needs to be synchronized
    SELECT lastid into …
Run Code Online (Sandbox Code Playgroud)

mysql synchronization stored-procedures locking boost-interprocess

8
推荐指数
2
解决办法
1万
查看次数

64位和32位进程互通boost :: message_queue

美好的一天,

我目前正试图找到一种在64位进程和32位进程之间传递数据的方法.由于它是一个实时应用程序,并且都在同一台计算机上运行,​​因此我很难使用共享内存(shm).

虽然我正在寻找一些使用shm的同步机制,但我觉得在boost :: message_queue上.然而,它不起作用.

我的代码基本上如下:

发件人部分

message_queue::remove("message_queue");
message_queue mq(create_only, "message_queue", 100, sizeof(uint8_t));
for (uint8_t i = 0; i < 100; ++i)
{
    mq.send(&i, sizeof(uint8_t), 0);
}
Run Code Online (Sandbox Code Playgroud)

接收器部分

message_queue mq(open_only, "message_queue");
for (uint8_t i = 0; i < 100; ++i)
{
    uint8_t v;
    size_t rsize;
    unsigned int rpriority;
    mq.receive(&v, sizeof(v), rsize, rpriority);
    std::cout << "v=" << (int) v << ", esize=" << sizeof(uint8_t) << ", rsize=" << rsize << ", rpriority=" << rpriority << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如果两个进程是64位或32位,则此代码可以正常工作.但如果两个过程不相同,则不起作用.

深入了解boost(1.50.0)代码,您将在message_queue_t :: do_receive(boost/interprocess/ipc/message_queue.hpp)中看到以下行: …

c++ boost boost-interprocess

8
推荐指数
1
解决办法
1962
查看次数

创建字符串的共享内存向量

我正在尝试创建一个管理(std)字符串的共享内存向量的类.

typedef boost::interprocess::allocator<std::string, boost::interprocess::managed_shared_memory::segment_manager> shmem_allocator;
typedef boost::interprocess::vector<std::string, shmem_allocator> shmem_vector;

shmem_mgr::shmem_mgr() :
    shmem_(create_only, SHMEM_KEY, SHMEM_SIZE),
    allocator_(shmem_.get_segment_manager())
{
    mutex_  = shmem_.find_or_construct<interprocess_mutex>(SHMEM_MUTEX)();
    condition_ = shmem_.find_or_construct<interprocess_condition>(SHMEM_CONDITION)();
    //buffer_ is of type shmem_vector
    buffer_  = shmem_.construct<shmem_vector>(SHMEM_BUFFER_KEY)(allocator_);
}

void shmem_mgr::run() {
    running_ = true;

    while(running_) {
        scoped_lock<interprocess_mutex> lock ( *mutex_ );

        int size = buffer_->size();

        log_.debug() << size << " queued request(s) found" << std::endl; //LINE 27
        for(int i=0; i<size; i++) {
            log_.debug() << buffer_->at(i); // at() crashes my app
        }

        buffer_->clear(); //so does clear()
        condition_->wait (lock); …
Run Code Online (Sandbox Code Playgroud)

c++ boost shared-memory boost-interprocess

8
推荐指数
1
解决办法
8502
查看次数

std :: vector是否满足Boost.Interprocess分配器的容器要求?

boost::interprocess文档中,据说要将容器存储在共享内存中:

  1. STL容器可能不会假定分配器分配的内存可以与其他相同类型的分配器一起释放.只有当分配了一个对象的内存可以与另一个对象解除分配时,所有分配器对象必须比较相等,并且这只能operator==()在运行时进行测试.
  2. 容器的内部指针应该是类型allocator::pointer,容器可能不会假定allocator::pointer是原始指针.
  3. 必须通过allocator::constructallocator::destroy函数构造所有对象.

我正在使用gcc 4.7.1和-std = c ++ 11(和boost 1.53).使用下面定义的ShmVector类型是否安全?

typedef boost::interprocess::allocator<int,
    boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;
Run Code Online (Sandbox Code Playgroud)

我尝试了一个使用这种类型的虚拟进程,看起来它正在工作,但我仍然不确定gcc4.7.1中的向量是否满足所有要求.我对第一个要求特别不确定.

#include <iostream>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <vector>
#include <cstdlib> //std::system

typedef boost::interprocess::allocator<int,
        boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;

int main(int argc, char *argv[])
{
    if(argc == 1){ //Parent process

        struct shm_remove
        {
            shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
            ~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
        } remover;

        //Create a new …
Run Code Online (Sandbox Code Playgroud)

c++ vector boost-interprocess gcc4.7

8
推荐指数
1
解决办法
692
查看次数

当其他进程可能正在使用它时删除boost interprocess_mutex

我试图用interprocess_mutexmanaged_windows_shared_memory.在我的项目中,多个进程class A在以下代码中创建实例.

using namespace boost::interprocess;

class A
{
    managed_windows_shared_memory* _shm;
    interprocess_mutex* _mtx;
}
A::A()
{
    _shm = new managed_windows_shared_memory{ open_or_create, "shm", 1024 };
    _mtx = _shm->find_or_construct<interprocess_mutex>("mtx")();
}
A::~A()
{
    delete _mtx;
    delete _shm;
}
Run Code Online (Sandbox Code Playgroud)

我可以看到,它是安全的呼吁delete _shm;~A(),因为managed_windows_shared_memory只有在使用它的每一个过程破坏了将被销毁managed_windows_shared_memory的对象,如写的文档.

不过,我不知道这是否是安全的呼吁delete _mtx;~A().在文档interprocess_mutex,没有提到它是否仍然被破坏,即使其他进程有对象引用它.

我搜索了这个,我猜我的选择是boost::interprocess::shared_ptr在这种情况下使用.我在这儿吗?这是我应该选择的吗?

c++ boost boost-interprocess

8
推荐指数
1
解决办法
455
查看次数

在另一个进程中销毁共享的std :: vector

我正在尝试std::vector<std::string>通过Boost.Interprocess 将a转移到新分叉的进程,以便子进程获取它的所有权并将其销毁.检索和读取矢量有效,但是我在破坏它时会遇到访问冲突.我认为分配器开始使用一些指向父母地址空间的指针,并且在孩子的地址空间中没有任何意义.

我应该如何在父母中创建该向量并在孩子中正确销毁?

父流程代码:

namespace ip = boost::interprocess;

template <class T>
using ip_allocator = ip::allocator<T, ip::managed_shared_memory::segment_manager>;

template <class T>
using ip_vector = std::vector<T, ip_allocator<T>>;

int CALLBACK WinMain(
    _In_ HINSTANCE,
    _In_ HINSTANCE,
    _In_ LPSTR,
    _In_ int
) {
    ip::shared_memory_object::remove("MyTestShm");
    ip::managed_shared_memory mshm{ ip::open_or_create, "MyTestShm", 1024 * 1024 };

    ip_allocator<int> intAlloc{ mshm.get_segment_manager() };

    mshm.construct<ip_vector<int>>("ForkData")(
        ip_vector<int>{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, intAlloc}
    );

    boost::process::spawn(L"Executor.exe", L"ForkData");
}
Run Code Online (Sandbox Code Playgroud)

子进程(Executor.exe)代码:

int wmain(int argc, wchar_t **argv) try {
    namespace …
Run Code Online (Sandbox Code Playgroud)

c++ boost-interprocess c++17

8
推荐指数
1
解决办法
219
查看次数

如何使用boost可升级互斥锁的示例

我有一个多线程服务器应用程序,需要在某些共享内存上进行互斥锁定.

共享内存基本上是sTL地图等.

很多时候我只是从地图上读书.但是,我还需要偶尔添加它.

例如typedef std :: map MessageMap; MessageMap msgmap; boost:shared_mutex access_;

void ProcessMessage(Message* message)
{
  //  Access message... read some stuff from it  message->...

  UUID id = message->GetSessionID();

  // Need to obtain a lock here. (shared lock? multiple readers)
  // How is that done?
  boost::interprocess::scoped_lock(access_);

  // Do some readonly stuff with msgmap
  MessageMap::iterator it = msgmap.find();
  // 

  // Do some stuff...

  // Ok, after all that I decide that I need to add an entry to the map.
  // …
Run Code Online (Sandbox Code Playgroud)

c++ boost-thread boost-interprocess

7
推荐指数
2
解决办法
1万
查看次数