#include #include #include #include #include #include #include #include namespace ip = boost::interprocess; typedef ip::managed_shared_memory::segment_manager segment_manager_t; typedef ip::allocator void_allocator; typedef ip::allocator uint8_allocator; typedef ip::vector uint8_vector; struct DataElementShared { uint64_t packet_id; uint8_vector data; DataElementShared(uint64_t id, int vector_length, const void_allocator &void_alloc) : packet_id(0), data(vector_length, uint8_t(), void_alloc) {} }; int main(int argc, char **argv) { // create sample data int number_of_samples = 5; int vector_size = 124609; std::vector datavec; for (int y = 0; y < vector_size; y++) { datavec.push_back(255); } int datavec_size = datavec.size() * sizeof(uint8_t); std::cout << "datavec_size: " << datavec_size << std::endl; ip::managed_shared_memory *managed_shm; try { ip::shared_memory_object::remove("shm_id"); managed_shm = new ip::managed_shared_memory( ip::create_only, "shm_id", (vector_size + 110) * (number_of_samples + 1)); } catch (ip::interprocess_exception &e) { if (e.get_error_code() != ip::no_error) { std::cout << "An exception occurred. Exception Nr. " << e.get_error_code() << e.what() << std::endl; std::cout << "bye-bye" << std::endl; return 0; } } std::cout << "shared memory created" << std::endl; // alias an STL-like allocator that uses the segment manager to allocate // memory typedef ip::allocator cb_alloc; // alias a circular buffer that uses the previous STL-like allocator typedef boost::circular_buffer cb; // initialize the STL-like allocator const cb_alloc alloc_inst(managed_shm->get_segment_manager()); // construct the circular buffer in the shared memory segment cb *circ_buffer = managed_shm->construct("circbuff")(number_of_samples, alloc_inst); std::cout << "circular_buffer with capacity " << circ_buffer->capacity() << " created" << std::endl; // initialize the circular buffer for (int c = 0; c < number_of_samples; c++) { DataElementShared dp(c, vector_size, alloc_inst); circ_buffer->push_back(dp); } std::cout << "circular buffer initialized" << std::endl; // write into shm until return key is pressed // launch async task to wait for keyboard input auto cinFuture = std::async(std::launch::async, []() { std::string in; std::getline(std::cin, in); return in; }); std::cout << "start writing into circular buffer..." << std::endl; uint64_t packet_id = 0; int writer_index = 0; do { circ_buffer->at(writer_index).packet_id = packet_id; std::memcpy(&circ_buffer->at(writer_index).data.front(), datavec.data(), datavec_size); // print packet id std::cout << "circ_buffer->at(" << writer_index << ").packet_id: " << circ_buffer->at(writer_index).packet_id << std::endl; // increment index writer_index = (writer_index + 1) % number_of_samples; packet_id++; } while (cinFuture.wait_for(std::chrono::milliseconds(500)) != std::future_status::ready); // free resources managed_shm->destroy("circbuff"); std::cout << "circular_buffer destroyed" << std::endl; ip::shared_memory_object::remove("shm_id"); std::cout << "shared memory deleted" << std::endl; std::cout << "bye-bye" << std::endl; return 0; }