Last active
January 16, 2023 08:29
-
-
Save sehe/9376856 to your computer and use it in GitHub Desktop.
Boost shared memory lockfree circular buffer queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)); | |
} | |
} |
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})
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
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