Skip to content

Instantly share code, notes, and snippets.

@sehe
Last active January 16, 2023 08:29
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save sehe/9376856 to your computer and use it in GitHub Desktop.
Save sehe/9376856 to your computer and use it in GitHub Desktop.
Boost shared memory lockfree circular buffer queue
#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;
}
#include <iostream>
int main()
{
// 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());
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";
}
}
all:consumer producer
CPPFLAGS+=-std=c++03 -Wall -pedantic
CPPFLAGS+=-g -O0
CPPFLAGS+=-isystem ~/custom/boost/
LDFLAGS+=-L ~/custom/boost/stage/lib/ -Wl,-rpath,/home/sehe/custom/boost/stage/lib
LDFLAGS+=-lboost_system -lrt -lpthread
%:%.cpp
$(CXX) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)
#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;
}
#include <unistd.h>
int main()
{
// 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 };
for (const char** msg_it = messages; *msg_it; ++msg_it)
{
sleep(1);
queue->push(shm::shared_string(*msg_it, char_alloc));
}
}
@blackham
Copy link

blackham commented Jan 4, 2017

I was able to build it.
boost: 1.56.0
c++: c++ (Gentoo 4.9.3 p1.5, pie-0.6.4) 4.9.3

I didn't use the Makefile. I just created a shell script:

#!/bin/sh

c++ -std=c++14 -lboost_system -lpthread -lrt consumer.cpp -o consumer
c++ -std=c++14 -lboost_system -lpthread -lrt producer.cpp -o producer

And thank you sehe for the quick easy example.

@nanugu
Copy link

nanugu commented May 31, 2017

Hi sehe,

Thanks for the example.

I am looking for a float array example instead of string array. Can you give an example how to code that? Basically what is the equivalent of basic_string for float array?

Thanks,
Narsi

Copy link

ghost commented Nov 7, 2017

With CMake:

cmake_minimum_required (VERSION 3.6.2 FATAL_ERROR)

find_package(Boost 1.64.0 REQUIRED COMPONENTS system)
include_directories(${Boost_INCLUDE_DIRS})

add_executable (consumer consumer.cpp)
target_link_libraries(consumer ${Boost_LIBRARIES})

add_executable (producer producer.cpp)
target_link_libraries(producer ${Boost_LIBRARIES})

@jomonjohnn
Copy link

Adding the updated CMakeLists.txt

cmake_minimum_required (VERSION 3.6.2 FATAL_ERROR)

SET (CMAKE_VERBOSE_MAKEFILE 0)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

find_package(Boost 1.64.0 REQUIRED COMPONENTS system)
include_directories(${Boost_INCLUDE_DIRS})

add_executable (consumer consumer.cpp)
target_link_libraries(consumer LINK_PUBLIC 
	${Boost_LIBRARIES}
	rt
	Threads::Threads
)

add_executable (producer producer.cpp)
target_link_libraries(producer LINK_PUBLIC 
	${Boost_LIBRARIES}
	rt
	Threads::Threads
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment