Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch boost message_queue returns false

Tags:

c++

boost

I am using boost::interprocess::message_queue and as per the definition given on 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);
  • Opens a previously created process shared message queue with name "name". If the was not previously created or there are no free resources, the function returns false.

now what i can't understand is that how a constructor is returning a value? though it states " the function returns false" but afaik message_queue is supposed to be a constructor.

and also if it do return false can i catch that in a Boolean variable?

like image 427
Hummingbird Avatar asked Jan 24 '26 07:01

Hummingbird


1 Answers

A boost::interprocess::interprocess_exception will be thrown instead, as the current documentation suggests.

So,

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

When run twice, will print

Error: #9, File exists
like image 136
sehe Avatar answered Jan 25 '26 21:01

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!