-
-
Save aleksas/08f832317e0e1b8d8efe0b5434cf362d to your computer and use it in GitHub Desktop.
Boost shared memory lockfree circular buffer queue
This file contains 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
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}) |
This file contains 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 "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())); | |
} | |
} |
This file contains 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 "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())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment