如何检查提升消息队列是否存在

How to check if boost message queue exists

本文关键字:消息 队列 是否 存在 何检查 检查      更新时间:2023-10-16

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

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

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

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

message_queue q(open_only,"q");

根据文档:

打开以前创建的名为"name"的进程共享消息队列。如果队列以前未创建或没有可用资源,则会引发错误。

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

一个简单的test_program向我展示了抛出interprocess_exception,错误代码为 7,表示not_found_error

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

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

具体来说,看一下链接的代码:

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