简体   繁体   English

在写入共享内存队列时出现段错误

[英]Segfault on writing to a Shared Memory Queue

I created boost deque on a shared memory using boost managed shared memory. 我使用boost管理的共享内存在共享内存上创建了boost deque。 I have one process ( process A ) that pushes data to the back of the queue and another process (process B )that reads from the front of the queue and pops the front. 我有一个进程(进程A)将数据推送到队列的后面,还有一个进程(进程B)从队列的前面读取并弹出前面。

My process A can push data into the queue without any issues. 我的进程A可以毫无问题地将数据推送到队列中。 But when I start my process B, it segfaults the moment it reads the front of the queue. 但是,当我启动进程B时,它在读取队列的开头时就会出现段错误。 process B can read the queue size correctly, it segfaults only when I read or pop an element from the queue. 进程B可以正确读取队列大小,只有当我从队列中读取或弹出一个元素时,它才会出现段错误。

Process A creates the shared memory and constructs the queue object. 进程A创建共享内存并构造队列对象。 While my process B finds the constructed object. 而我的过程B找到了构造的对象。

My SharedMemQueue is, 我的SharedMemQueue是,

struct QueueType
{       
   boost::interprocess::interprocess_mutex mutex;
   boost::interprocess::interprocess_condition signal;
   boost::interprocess::deque < int > queue;
};


class ShmQueue
{
public:
    ShmQueue ( std::string name )
    :   _shm_name ( name )
    {
    }
    ~ShmQueue ( )
    {
        Deinit();
    }
    bool Init ( bool value = false )
    {
        bool result ( false );
        try
        {
            if ( value )
            {
                shared_memory_object::remove ( _shm_name.c_str() );
                _shm_mem = managed_shared_memory ( create_only, _shm_name.c_str(), 65356 );
                _queue = _shm_mem.construct < QueueType > ( "Queue" )();
            }
            else
            {
                _shm_mem = managed_shared_memory ( open_only, _shm_name.c_str() );
                _queue = _shm_mem.find < QueueType > ( "Queue" ).first;
            }
        }
        catch ( interprocess_exception &e )
        {
            std::cout << e.what() << std::endl;
            _queue = NULL;
        }
        if ( _queue != NULL )
        {
            result = true;
        }
        return result;
    }

    bool Deinit ( )
    {
        bool result ( false );
        _shm_mem.destroy < QueueType > ( "Queue" );
        shared_memory_object::remove ( _shm_name.c_str() );
        return result;
    }
    void Push ( int value )
    {
        scoped_lock < interprocess_mutex > lock ( _queue->mutex );
        if ( _queue->queue.size ( ) < 20 )
        {
            _queue->queue.push_back ( value );
        }
    }

    int Pop ( )
    {
        int result ( -1 );
        scoped_lock < interprocess_mutex > lock ( _queue->mutex );
        if ( !_queue->queue.empty( ) )
        {
            result = _queue->queue.front();
            _queue->queue.pop_front();
        }
        return result;
    }

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

Any help is much appreciated, Thanks 任何帮助都非常感谢,谢谢

If you can read the size of queue but not its ellements, than its probably that queue is just a handler and stores its ellements somewhere else. 如果您可以读取队列的大小但不能读取其元素,则该队列可能只是一个处理程序,并将其元素存储在其他位置。 Are you sure that boost::interprocess::deque < int > queue; 您确定boost::interprocess::deque < int > queue; uses your shared memory to allocate its ellements? 使用您的共享内存来分配其元素?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM