Skip to content

Instantly share code, notes, and snippets.

@aleksas
Forked from sehe/Makefile
Last active November 12, 2020 22:30
Show Gist options
  • Save aleksas/08f832317e0e1b8d8efe0b5434cf362d to your computer and use it in GitHub Desktop.
Save aleksas/08f832317e0e1b8d8efe0b5434cf362d to your computer and use it in GitHub Desktop.
Boost shared memory lockfree circular buffer queue
cmake_minimum_required(VERSION 3.0.0)
project(circular-buffer-queue VERSION 0.1.0)
set(Boost_USE_MULTITHREADED OFF)
add_definitions(-DBOOST_ALL_NO_LIB)
find_package( Boost 1.54 COMPONENTS system thread REQUIRED )
include_directories(${Boost_INCLUDE_DIRS})
set(LINK_LIBRARIES
rt
pthread
${Boost_LIBRARIES})
add_executable(circular-buffer-queue-consumer consumer.cpp)
target_link_libraries(circular-buffer-queue-consumer ${LINK_LIBRARIES})
add_executable(circular-buffer-queue-producer producer.cpp)
target_link_libraries(circular-buffer-queue-producer ${LINK_LIBRARIES})
#include "shared_memory_ring_buffer.h"
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/random.hpp>
int main() {
boost::mt19937 generator;
boost::uniform_int<> range( 10, 30 );
boost::variate_generator< boost::mt19937, boost::uniform_int<> > nextRandomValue(generator, range);
bip::managed_shared_memory segment(bip::open_or_create, "MySharedMemory", 65536);
shm::char_alloc char_alloc(segment.get_segment_manager());
shm::ring_buffer *queue = segment.find_or_construct<shm::ring_buffer>("queue")();
while (true) {
shm::shared_string v(char_alloc);
if (queue->pop(v))
std::cout << "Processed: '" << v << "'\n";
boost::this_thread::sleep(boost::posix_time::milliseconds(nextRandomValue()));
}
}
#include "shared_memory_ring_buffer.h"
#include <unistd.h>
#include <boost/thread/thread.hpp>
#include <boost/random.hpp>
int main() {
boost::mt19937 generator;
boost::uniform_int<> range( 50, 100 );
boost::variate_generator< boost::mt19937, boost::uniform_int<> > nextRandomValue(generator, range);
// create segment and corresponding allocator
bip::managed_shared_memory segment(bip::open_or_create, "MySharedMemory", 65536);
shm::char_alloc char_alloc(segment.get_segment_manager());
// Ringbuffer fully constructed in shared memory. The element strings are
// also allocated from the same shared memory segment. This vector can be
// safely accessed from other processes.
shm::ring_buffer *queue = segment.find_or_construct<shm::ring_buffer>("queue")();
const char* messages[] = { "hello world", "the answer is 42", "where is your towel", 0 };
while (true) {
for (const char** msg_it = messages; *msg_it; ++msg_it) {
queue->push(shm::shared_string(*msg_it, char_alloc));
}
boost::this_thread::sleep(boost::posix_time::milliseconds(nextRandomValue()));
}
}
#ifndef SHARED_MEMORY_RING_BUFFER_H
#define SHARED_MEMORY_RING_BUFFER_H
#include <boost/lockfree/spsc_queue.hpp> // ring buffer
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
namespace bip = boost::interprocess;
namespace shm
{
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> char_alloc;
typedef bip::basic_string<char, std::char_traits<char>, char_alloc > shared_string;
typedef boost::lockfree::spsc_queue<
shared_string,
boost::lockfree::capacity<200>
> ring_buffer;
}
#endif // SHARED_MEMORY_RING_BUFFER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment