Created
November 16, 2012 04:30
-
-
Save hank/4084074 to your computer and use it in GitHub Desktop.
Simple Boost.Interprocess Demo
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/interprocess/managed_shared_memory.hpp> | |
#include <cstdlib> | |
#include <sstream> | |
int main(int argc, char** argv) | |
{ | |
namespace ip = boost::interprocess; | |
ip::managed_shared_memory segment(ip::open_only, "Erik"); | |
ip::managed_shared_memory::handle_t handle = 0; | |
std::stringstream s; | |
s << argv[1]; | |
s >> handle; | |
void* message = segment.get_address_from_handle(handle); | |
printf("Message: %s\n", (char*)message); | |
segment.deallocate(message); | |
return EXIT_SUCCESS; | |
} |
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/interprocess/managed_shared_memory.hpp> | |
#include <cstdlib> | |
#include <sstream> | |
#define ALLOC_BYTES 1024 | |
int main() | |
{ | |
namespace ip = boost::interprocess; | |
struct shm_remove | |
{ | |
shm_remove() { ip::shared_memory_object::remove("Erik"); } | |
~shm_remove() { ip::shared_memory_object::remove("Erik"); } | |
} remover; | |
ip::managed_shared_memory segment(ip::create_only, "Erik", 32768); | |
std::size_t free_memory = segment.get_free_memory(); | |
void * shptr = segment.allocate(ALLOC_BYTES); | |
if(free_memory <= segment.get_free_memory()) | |
{ | |
return EXIT_FAILURE; | |
} | |
strcpy((char*)shptr, "Manatees!"); | |
ip::managed_shared_memory::handle_t handle = | |
segment.get_handle_from_address(shptr); | |
std::cout << "Handle is " << handle << std::endl; | |
pause(); | |
return EXIT_SUCCESS; | |
} |
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
CFLAGS=-O3 -Wall | |
TARGETS=example1_server example1_client | |
all: $(TARGETS) | |
example1_server: example1_server.cpp | |
$(CXX) $(CFLAGS) -o $@ $< | |
example1_client: example1_client.cpp | |
$(CXX) $(CFLAGS) -o $@ $< | |
clean: | |
rm -f $(TARGETS) |
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
hank@shelob ~/repos/life/code/cpp/boost/interproc $ ./example1_server & | |
[1] 55037 | |
hank@shelob ~/repos/life/code/cpp/boost/interproc $ Handle is 192 | |
hank@shelob ~/repos/life/code/cpp/boost/interproc $ ./example1_client 192 | |
Message: Manatees! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment