2

我试图学习如何在共享内存中创建一个对象,以便多个进程可以写入/读取共享队列。当一个进程写入队列时,它可以使用 boost 条件变量向其他进程发出信号。我对共享内存应用程序不太熟悉,所以请更正我的代码。

我的对象如下图所示,

typedef boost::interprocess::vector < uint8_t > buffer_t;

struct QueueType
{
    boost::interprocess::interprocess_condition Signal;
    boost::interprocess::interprocess_mutex Mutex;
    boost::interprocess::deque < buffer_t > Queue;
};

我分配上述类的对象的代码如下,

managed_shared_memory _shm_mem;
std::string _shm_name;
QueueType * _queue;

bool Start()
{
    bool result ( false );
    try 
    {
        _shm_mem = managed_shared_memory ( open_or_create, _shm_name.c_str(), 65536);   
        _queue = _shm_mem.find_or_construct<QueueType>("Queue")();
        if ( _queue != NULL ) 
        {
           result = true;
        }   
    }
    catch (boost::interprocess::interprocess_exception &e )
    {
        std::cout << e.what() << std::endl;
    }
    return result;
}

使用上面的代码我无法获得有效的 _queue 对象。

4

0 回答 0