如何捕获boost消息队列返回false

how to catch boost message_queue returns false

本文关键字:返回 false 队列 消息 何捕获 boost      更新时间:2023-10-16

我正在使用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,我能在布尔变量中捕捉到吗?

如当前文档所示,将抛出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