2

I'm using boost message_queue and I create the queue in one c++ program and use it in another program.

My problem is, that sometimes the first program doesn't run yet, but the second is running.

So when I start first program, I want to know, if the queue exists. I don't want to use message_queue::remove() because I don't want to lose some data.

The question is, how can I know if message_queue "bla_bla_queue" exists, or not?

message_queue q(open_only,"q");
0

2 Answers 2

3

According to the doc:

Opens a previously created process shared message queue with name "name". If the queue was not previously created or there are no free resources, throws an error.

So you should be able to catch an exception if the message queue does not exist.

A simple test_program showed me, that the interprocess_exception is thrown and the error code is 7, which indicates a not_found_error.

Sign up to request clarification or add additional context in comments.

1 Comment

Not satisfied with the answer? Tell me why ;) (to the person who downvoted it)
2

Create it and surround with try and catch. Read the doc to find the error code (for your specific version of boost) for already_exists (or something like that)

Check Boost 1.55 doc for an example in that version

Specifically, take a look at the linked code:

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;
  }
}

there is an

already_exists_error

4 Comments

Better use boost.org/doc/libs/1_55_0/doc/html/interprocess/… as link and maybe the latest version?! (which would be boost.org/doc/libs/1_67_0/doc/html/interprocess/… )
My experience only goes to boost 1.55 so far. Did not dare to assume it will stay the same in the latest version
"there is an already_exists_error". He wants to know, if the message queue does not exist :)
But i declare it out of function in cpp file, and i can't use try catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.