|
| 1 | +#include <boost/interprocess/managed_shared_memory.hpp> |
| 2 | +#include <boost/interprocess/containers/vector.hpp> |
| 3 | +#include <boost/interprocess/allocators/allocator.hpp> |
| 4 | +#include <boost/interprocess/containers/string.hpp> |
| 5 | +#include <cstdlib> //std::system |
| 6 | +#include <jsoncons/json.hpp> |
| 7 | + |
| 8 | +using namespace jsoncons; |
| 9 | + |
| 10 | +typedef boost::interprocess::allocator<int, |
| 11 | + boost::interprocess::managed_shared_memory::segment_manager> shmem_allocator; |
| 12 | + |
| 13 | +struct boost_sorted_policy : public sorted_policy |
| 14 | +{ |
| 15 | + template <class T, class Allocator> |
| 16 | + using sequence_container_type = boost::interprocess::vector<T,Allocator>; |
| 17 | + |
| 18 | + template <class CharT, class CharTraits, class Allocator> |
| 19 | + using key_storage = boost::interprocess::basic_string<CharT, CharTraits, Allocator>; |
| 20 | + |
| 21 | + template <class CharT, class CharTraits, class Allocator> |
| 22 | + using string_storage = boost::interprocess::basic_string<CharT, CharTraits, Allocator>; |
| 23 | +}; |
| 24 | + |
| 25 | +typedef basic_json<char,boost_sorted_policy,shmem_allocator> shm_json; |
| 26 | + |
| 27 | +int main(int argc, char *argv[]) |
| 28 | +{ |
| 29 | + typedef std::pair<double, int> MyType; |
| 30 | + |
| 31 | + if(argc == 1){ //Parent process |
| 32 | + //Remove shared memory on construction and destruction |
| 33 | + struct shm_remove |
| 34 | + { |
| 35 | + shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); } |
| 36 | + ~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); } |
| 37 | + } remover; |
| 38 | + |
| 39 | + //Construct managed shared memory |
| 40 | + boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, |
| 41 | + "MySharedMemory", 65536); |
| 42 | + |
| 43 | + //Initialize shared memory STL-compatible allocator |
| 44 | + const shmem_allocator allocator(segment.get_segment_manager()); |
| 45 | + |
| 46 | + // Create json value with all dynamic allocations in shared memory |
| 47 | + |
| 48 | + shm_json* j = segment.construct<shm_json>("my json")(shm_json::array(allocator)); |
| 49 | + j->push_back(10); |
| 50 | + |
| 51 | + shm_json o(allocator); |
| 52 | + //o.try_emplace("category", "reference",allocator); |
| 53 | + //o.try_emplace("author", "Nigel Rees",allocator); |
| 54 | + //o.try_emplace("title", "Sayings of the Century",allocator); |
| 55 | + //o.try_emplace("price", 8.95, allocator); |
| 56 | + o.insert_or_assign("category", "reference"); |
| 57 | + o.insert_or_assign("author", "Nigel Rees"); |
| 58 | + o.insert_or_assign("title", "Sayings of the Century"); |
| 59 | + o.insert_or_assign("price", 8.95); |
| 60 | + |
| 61 | + j->push_back(o); |
| 62 | + |
| 63 | + shm_json a = shm_json::array(2,shm_json::object(allocator),allocator); |
| 64 | + a[0]["first"] = 1; |
| 65 | + |
| 66 | + j->push_back(a); |
| 67 | + |
| 68 | + std::pair<shm_json*, boost::interprocess::managed_shared_memory::size_type> res; |
| 69 | + res = segment.find<shm_json>("my json"); |
| 70 | + |
| 71 | + std::cout << "Parent:" << std::endl; |
| 72 | + std::cout << pretty_print(*(res.first)) << std::endl; |
| 73 | + |
| 74 | + //Launch child process |
| 75 | + std::string s(argv[0]); s += " child "; |
| 76 | + if(0 != std::system(s.c_str())) |
| 77 | + return 1; |
| 78 | + |
| 79 | + |
| 80 | + //Check child has destroyed all objects |
| 81 | + if(segment.find<MyType>("my json").first) |
| 82 | + return 1; |
| 83 | + } |
| 84 | + else{ |
| 85 | + //Open managed shared memory |
| 86 | + boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, |
| 87 | + "MySharedMemory"); |
| 88 | + |
| 89 | + std::pair<shm_json*, boost::interprocess::managed_shared_memory::size_type> res; |
| 90 | + res = segment.find<shm_json>("my json"); |
| 91 | + |
| 92 | + if (res.first != nullptr) |
| 93 | + { |
| 94 | + std::cout << "Child:" << std::endl; |
| 95 | + std::cout << pretty_print(*(res.first)) << std::endl; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + std::cout << "Result is null" << std::endl; |
| 100 | + } |
| 101 | + |
| 102 | + //We're done, delete all the objects |
| 103 | + segment.destroy<shm_json>("my json"); |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
0 commit comments