I have this writer, I run this like so: ./writer 12 14
it creates two shared memory segments with a spsc queue in each one.
The writer just send text with a counter to the spsc queue of his memory segment.
#include <boost/container/scoped_allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/process.hpp>
#include <iostream>
using namespace std::chrono_literals;
namespace bip = boost::interprocess;
namespace chro = std::chrono;
namespace blf = boost::lockfree;
using char_alloc = bip::allocator<char, bip::managed_shared_memory::segment_manager>;
using shared_string = bip::basic_string<char, std::char_traits<char>, char_alloc>;
using ring_buffer = blf::spsc_queue<shared_string, blf::capacity<200>>;
int main(int argc, char* argv[]) {
if (argc > 1) {
std::string n1 = argv[1];
std::string n2 = argv[2];
const std::string shm_name1 = "segmentrb" + n1;
const std::string shm_name2 = "segmentrb" + n2;
const std::string qname = "queue";
boost::interprocess::shared_memory_object::remove(shm_name1.c_str());
boost::interprocess::shared_memory_object::remove(shm_name2.c_str());
bip::managed_shared_memory seg1(bip::open_or_create, shm_name1.c_str(), 10'000);
char_alloc char_alloc1(seg1.get_segment_manager());
ring_buffer* qu1 = seg1.find_or_construct<ring_buffer>(qname.c_str())();
bip::managed_shared_memory seg2(bip::open_or_create, shm_name2.c_str(), 10'000);
char_alloc char_alloc2(seg2.get_segment_manager());
ring_buffer* qu2 = seg2.find_or_construct<ring_buffer>(qname.c_str())();
int counter = 0;
while (true) {
std::string text1 = "Text from 1, count ";
text1.append(std::to_string(counter));
qu1->push(shared_string(text1.c_str(), char_alloc1));
std::string text2 = "Text from 2, count ";
text2.append(std::to_string(counter));
qu2->push(shared_string(text2.c_str(), char_alloc2));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
counter++;
}
}
}
Then I have this reader, that reads an pop the spsc of the two segments:
I run this with: ./reader 12 14
#include <boost/container/scoped_allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/process.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
using namespace std::chrono_literals;
namespace bip = boost::interprocess;
namespace chro = std::chrono;
namespace blf = boost::lockfree;
using char_alloc = bip::allocator<char, bip::managed_shared_memory::segment_manager>;
using shared_string = bip::basic_string<char, std::char_traits<char>, char_alloc>;
using ring_buffer = blf::spsc_queue<shared_string, blf::capacity<200>>;
int main(int argc, char* argv[]) {
if (argc > 1) {
std::string n1 = argv[1];
std::string n2 = argv[2];
const std::string shm_name1 = "segmentrb" + n1;
const std::string shm_name2 = "segmentrb" + n2;
const std::string qname = "queue";
bip::managed_shared_memory seg1(bip::open_only, shm_name1.c_str());
char_alloc char_alloc1(seg1.get_segment_manager());
ring_buffer* qu1 = seg1.find<ring_buffer>(qname.c_str()).first;
bip::managed_shared_memory seg2(bip::open_only, shm_name2.c_str());
char_alloc char_alloc2(seg2.get_segment_manager());
ring_buffer* qu2 = seg2.find<ring_buffer>(qname.c_str()).first;
while (true) {
shared_string v1(char_alloc1);
shared_string v2(char_alloc2);
qu1->pop(v1);
qu2->pop(v2);
long lv1 = v1.length();
long lv2 = v2.length();
long lvs = lv1 + lv2;
if (lvs > 0) {
if (lv1 > 0) {
std::cout << "Rec1: " << v1 << "\n";
}
if (lv2 > 0) {
std::cout << "Rec2: " << v2 << "\n";
}
}
else {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
}
}
when I do kill -9 on the reader I get this on the writer:
terminate called after throwing an instance of 'boost::interprocess::bad_alloc'
what(): boost::interprocess::bad_alloc
Aborted (core dumped)
How can I avoid the writer being killed?