BOOST库 消息队列 紧急注意事项 what(): boost::interprocess_exception::library_error


直接贴实验代码:

/*******         boost 消息队列          **********/
#if 1  
#include 
#include    
#include    
#include    
#include 

using namespace boost::interprocess;  
  


void thread_send() 
{ 
   try{  
      //Erase previous message queue   
      message_queue::remove("message_queue");  
  
      //Create a message_queue.   
      message_queue mq  
         (create_only               //only create   
         ,"message_queue"           //name   
         ,10                       //max message number   
         ,sizeof(int)               //max message size   
         );  
  
      //Send 10 numbers   
      for(int i = 0; i < 10; ++i){  
         mq.send(&i, sizeof(i), 0);  
      }  
   }  
   catch(interprocess_exception &ex){  
      std::cout << ex.what() << std::endl;  
      return;  
   } 

   return;   
} 


void thread_recv() 
{ 
   try{  
      //Open a message queue.   
      message_queue mq  
         (open_only        //only create   
         ,"message_queue"  //name   
         );  
  
      unsigned int priority;  
      message_queue::size_type recvd_size;  
  
      //Receive 10 numbers   
      for(int i = 0; i < 10; ++i){  
         int number_recv;  

         mq.receive(&number_recv, sizeof(int), recvd_size, priority); 
         if(number_recv != i || recvd_size != sizeof(int))  
            return;  
         else
           std::cout << "recv data : " <<  number_recv << std::endl;
      }  
   }  
   catch(interprocess_exception &ex){  
      message_queue::remove("message_queue");  
      std::cout << ex.what() << std::endl;  
      return;  
   }  
   message_queue::remove("message_queue");  
   return;  
}


int main() 
{ 
   boost::thread t_send(thread_send); 
   boost::thread t_recv(thread_recv); 

   t_send.detach(); 
   t_recv.detach(); 

  while(1){

     sleep(3);
     std::cout << " --run--"<< std::endl;  

  }

  return 0;
}

makefile:

.PHONY: DOIT

DOIT:
    mips-linux-gnu-g++ -I.  my_boost_test.cpp -L./lib  -lboost_thread -lboost_system -o boost_app  -lrt -lpthread

后记,BOOST库消息队列使用的注意事项

下面的代码片段会有问题

片段1:

      //Create a message_queue.   
      message_queue mq1  
        (create_only,       //only create   
         mq_event.c_str() ,         //name   
         2,                        //max message number   
         2*sizeof(int)       max message size   这里是2倍的sizeof(int)
         ); 

     p_mq_event =  new  message_queue       //Open a message queue.    
                        (open_only,
                        mq_event.c_str()     //name   
                        ); 

片段2:

        unsigned int priority;  
        message_queue::size_type recvd_size;  
        int event_t_recv;  
        p_mq_event->receive(&event_t_recv, sizeof(int), recvd_size, priority);  这里第二个参数是sizeof(int) 
        if(recvd_size != len*sizeof(int)) {
            std::cout << "wait_event(): false " << std::endl;
            assert(0);
            return false;  

        }else{
            r_event_t = event_t_recv;
            std::cout << "wait_event(): event_t = " << event_t_recv << std::endl;
            return true;  
        }

运行效果:

BOOST库使用消息队列的容易出错的点:

max message size 值需要和调用receive 或 send方法时的第二个参数大小一样。否则就会报上述错误。