【问题标题】:How to check if boost message queue exists如何检查提升消息队列是否存在
【发布时间】:2018-11-23 04:16:07
【问题描述】:

我正在使用 boost message_queue,我在一个 c++ 程序中创建队列并在另一个程序中使用它。

我的问题是,有时第一个程序尚未运行,但第二个程序正在运行。

所以当我启动第一个程序时,我想知道队列是否存在。 我不想使用 message_queue::remove() 因为我不想丢失一些数据。

问题是,我怎么知道 message_queue "bla_bla_queue" 是否存在?

message_queue q(open_only,"q");

【问题讨论】:

    标签: c++ boost message-queue


    【解决方案1】:

    根据doc

    打开一个以前创建的名为“name”的进程共享消息队列。如果队列之前没有创建或没有可用资源,则抛出错误。

    因此,如果消息队列不存在,您应该能够捕获异常。

    simple test_program 告诉我,interprocess_exception 被抛出,错误代码为 7,表示not_found_error

    【讨论】:

    • 对答案不满意?告诉我为什么;)(对投反对票的人)
    【解决方案2】:

    创建它并用trycatch 包围。阅读文档以查找 already_exists (或类似内容)的错误代码(针对您特定版本的 boost)

    查看Boost 1.55 doc 以获取该版本中的示例

    具体看一下链接代码:

    namespace boost {
      namespace interprocess {
    
        enum error_code_t { no_error = = 0, system_error, other_error, 
                            security_error, read_only_error, io_error, path_error, 
                            not_found_error, busy_error, already_exists_error, 
                            not_empty_error, is_directory_error, 
                            out_of_space_error, out_of_memory_error, 
                            out_of_resource_error, lock_error, sem_error, 
                            mode_error, size_error, corrupted_error, 
                            not_such_file_or_directory, invalid_argument, 
                            timeout_when_locking_error, 
                            timeout_when_waiting_error };
    
        typedef int native_error_t;
      }
    }
    

    有一个

    already_exists_error
    

    【讨论】:

    猜你喜欢
    • 2014-06-18
    • 2023-04-08
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2020-11-27
    • 2012-09-14
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多