【问题标题】:how to catch boost message_queue returns false如何捕获 boost message_queue 返回 false
【发布时间】:2014-12-07 02:59:54
【问题描述】:

我正在使用 boost::interprocess::message_queue 并根据给出的定义 http://www.boost.org/doc/libs/1_35_0/doc/html/boost/interprocess/message_queue.html

message_queue(open_only_t open_only, const char * name);
  • 打开以前创建的名为“name”的进程共享消息队列。如果之前未创建或没有可用资源,则该函数返回 false。

现在我无法理解的是构造函数如何返回值?虽然它声明“函数返回false”但afaik message_queue应该是一个构造函数。

如果它确实返回 false,我可以在布尔变量中捕获它吗?

【问题讨论】:

标签: c++ boost


【解决方案1】:

正如当前文档所建议的那样,将改为抛出 boost::interprocess::interprocess_exception

所以,

using namespace boost::interprocess;
try {
    //Create a message_queue. If the queue
    //exists throws an exception
    message_queue mq
        (create_only         //only create
         ,"message_queue"     //name
         ,100                 //max message number
         ,100                 //max message size
        );
} catch (interprocess_exception const& ipe)
{
    std::cerr << "Error: #" << ipe.get_error_code() << ", " << ipe.what() << "\n";
}

当运行两次时,将打印

Error: #9, File exists

【讨论】:

  • 是的..我现在也在做同样的事情,但不幸的是我引用了旧文档,似乎说构造函数“返回错误”,这就是困扰我的地方。无论如何感谢您的回答。
猜你喜欢
  • 2018-08-29
  • 2017-08-11
  • 2021-12-08
  • 2021-08-25
  • 2016-12-01
  • 2011-09-06
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多