test_9192.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2014 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/interprocess/shared_memory_object.hpp>
  6. #include <boost/interprocess/mapped_region.hpp>
  7. #include <boost/thread.hpp>
  8. using namespace boost::interprocess;
  9. struct item
  10. {
  11. int i;
  12. };
  13. struct queue
  14. {
  15. void put( const item& item )
  16. {
  17. boost::unique_lock<boost::mutex> lock(mutex);
  18. while ( item_in )
  19. cond_full.wait(lock);
  20. item_ = item;
  21. item_in = true;
  22. cond_empty.notify_one();
  23. }
  24. void print()
  25. {
  26. boost::unique_lock<boost::mutex> lock(mutex);
  27. while ( !item_in )
  28. cond_empty.wait(lock);
  29. item_in = false;
  30. std::cout << item_.i << std::endl;
  31. cond_full.notify_one();
  32. }
  33. private:
  34. //Mutex to protect access to the queue
  35. boost::mutex mutex;
  36. //Condition to wait when the queue is empty
  37. boost::condition_variable cond_empty;
  38. //Condition to wait when the queue is full
  39. boost::condition_variable cond_full;
  40. bool item_in;
  41. //Items to fill
  42. item item_;
  43. };
  44. void *addr;
  45. void printThread()
  46. {
  47. //Erase previous shared memory and schedule erasure on exit
  48. struct shm_remove
  49. {
  50. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  51. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  52. } remover;
  53. //Create a shared memory object.
  54. shared_memory_object shm(create_only,"MySharedMemory",read_write);
  55. try
  56. {
  57. // //Set size
  58. // shm.truncate(sizeof(queue));
  59. //
  60. // //Map the whole shared memory in this process
  61. // mapped_region region(shm,read_write);
  62. //
  63. // //Get the address of the mapped region
  64. // void *addr = region.get_address();
  65. //Construct the shared structure in memory
  66. queue *q = new (addr) queue;
  67. do
  68. {
  69. q->print();
  70. } while ( true );
  71. }
  72. // catch(interprocess_exception &ex)
  73. // {
  74. // std::cout << ex.what() << std::endl;
  75. // }
  76. catch(boost::thread_interrupted&)
  77. {
  78. std::cout << "interrupted" << std::endl;
  79. }
  80. catch(...)
  81. {
  82. std::cout << "exception" << std::endl;
  83. }
  84. }
  85. int main()
  86. {
  87. addr = new queue();
  88. boost::thread t(printThread);
  89. // give the thread time to create the shm
  90. boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
  91. // //Create a shared memory object.
  92. // shared_memory_object shm(open_only,"MySharedMemory",read_write);
  93. try
  94. {
  95. // //Map the whole shared memory in this process
  96. // mapped_region region(shm,read_write);
  97. //
  98. // //Get the address of the mapped region
  99. // void *addr = region.get_address();
  100. //Obtain a pointer to the shared structure
  101. queue *q = static_cast<queue*>(addr);
  102. item i;
  103. i.i = 42;
  104. q->put( i );
  105. ++i.i;
  106. q->put( i );
  107. // give the printThread time to "process" the item
  108. boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
  109. t.interrupt();
  110. t.join();
  111. }
  112. catch(...)
  113. {
  114. std::cout << "exception" << std::endl;
  115. return -1;
  116. }
  117. }