comp_doc_message_queueB.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. //[doc_message_queueB
  12. #include <boost/interprocess/ipc/message_queue.hpp>
  13. #include <iostream>
  14. #include <vector>
  15. using namespace boost::interprocess;
  16. int main ()
  17. {
  18. try{
  19. //Open a message queue.
  20. message_queue mq
  21. (open_only //only create
  22. ,"message_queue" //name
  23. );
  24. unsigned int priority;
  25. message_queue::size_type recvd_size;
  26. //Receive 100 numbers
  27. for(int i = 0; i < 100; ++i){
  28. int number;
  29. mq.receive(&number, sizeof(number), recvd_size, priority);
  30. if(number != i || recvd_size != sizeof(number))
  31. return 1;
  32. }
  33. }
  34. catch(interprocess_exception &ex){
  35. message_queue::remove("message_queue");
  36. std::cout << ex.what() << std::endl;
  37. return 1;
  38. }
  39. message_queue::remove("message_queue");
  40. return 0;
  41. }
  42. //]
  43. #include <boost/interprocess/detail/config_end.hpp>