gpt4 book ai didi

c++ - linux mq_open 忽略 mq_msgsize 属性

转载 作者:太空狗 更新时间:2023-10-29 12:35:30 25 4
gpt4 key购买 nike

所有,以前认为我是理智的,现在不太确定了。
我正在尝试创建一个消息队列,其 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com