c++ - linux mq_open 忽略 mq_msgsize 属性

标签 c++ linux mqueue

所有,以前认为我是理智的,现在不太确定了。
我正在尝试创建一个消息队列,其 mq_msgsize 属性不是 8192,这似乎是默认值。我在下面附上了我的代码——它有许多显示值的 printf。如果您能指出我做错了什么,我将永远感激不已。

bool Subscriber::Subscribe( void )
{
   mqd_t  qid;
   bool   brv = false;
   msg_topic_t topic = this->GetTopic();
   struct mq_attr q_attr;
   int rv = 0;

   if (VALID_TOPIC( topic ))
   {
        if (this->GetName().length() > 0)
        {
            string qnamestr = this->GetName();
            if (qnamestr[0] != '/')
            {   
                qnamestr = "/" + qnamestr; 
                this->SetName(qnamestr);
            }
            const char * qname = (const char *) qnamestr.c_str();

            q_attr.mq_msgsize = 256;
            q_attr.mq_curmsgs = 0;
            q_attr.mq_flags = O_NONBLOCK;
            q_attr.mq_maxmsg = 10;

            qid = mq_open( qname, O_RDONLY|O_CREAT, 0644, &q_attr );
            if ((mqd_t) -1 != qid)
            {   
                rv = mq_getattr(qid, &q_attr );
                if (rv != 0)
                {   perror(" get_attr1 failed: "); }
                printf(" queue size is now: %d\n", q_attr.mq_msgsize);
                if (q_attr.mq_msgsize > 1024)
                {
                    struct mq_attr old_attr;

                    q_attr.mq_msgsize = 1024;
                    rv = mq_setattr( qid, &q_attr, &old_attr);
                    if (rv != 0)
                    {   perror(" could not update message size: "); }
                    rv = mq_getattr(qid, &q_attr );
                    if (rv != 0)
                    {   perror(" get_attr2 failed: "); }
                    printf(" queue size is now: %d\n", q_attr.mq_msgsize);
                }
                this->SetOutboxID( qid );
                brv = true; 
                DLOG(INFO) << " qid = " << qid << endl;
                MessageCenter * mc = MessageCenter::GetInstance();
                mc->AddSubscriber( (Subscriber *) this );
            }
        }
    }
   drain_queue( this->GetOutboxID());
    return( brv );
}

输出如下所示: 队列大小现在是:8192 队列大小现在是:8192 队列大小现在是:8192

谢谢!

最佳答案

联机帮助页说:

The mq_maxmsg and mq_msgsize fields are set when the message queue is created by mq_open(3)

The only attribute that can be modified is the setting of the O_NONBLOCK flag in mq_flags. The other fields in newattr are ignored.

关于c++ - linux mq_open 忽略 mq_msgsize 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589379/

相关文章:

ipc - POSIX 消息队列文件夹

c++ - 影响帧率的 allegro 5 线程

C++继承模式

python - 无法使用Python调用GDB用户定义的函数

linux - gcc __sync_bool_compare_and_swap 和 cmpxchg 之间有什么区别?

c - posix mqueue 中的错误文件描述符

C mqueue 不发送或接收整个消息

c++ - 从动态数组中删除一个字符串并使该数组更小

c++ - c++ 中的 const 和没有 const 方法?

python - 在基于 RedHat 的环境中,Python 3.5 上的 cx_oracle?