Skip to content

Instantly share code, notes, and snippets.

@AyumiKasagi
Created November 3, 2017 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AyumiKasagi/f8d421a56e5d428f9ebe353c87f40882 to your computer and use it in GitHub Desktop.
Save AyumiKasagi/f8d421a56e5d428f9ebe353c87f40882 to your computer and use it in GitHub Desktop.
共有メモリを用いて画像をプロセス間でやりとりします。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <unistd.h>
#include <opencv2/opencv.hpp>
using namespace boost::interprocess;
typedef allocator<unsigned char, managed_shared_memory::segment_manager> shmalloc;
typedef vector<unsigned char, shmalloc> shmimage;
int main( int argc, char* argv[] )
{
managed_shared_memory shm(open_only, "NAME");
shmimage *image = shm.find< shmimage >("Image").first;
cv::Mat_<cv::Vec3b> im(480, 640);
shmimage& imref = *image;
memcpy(im.data, &imref[0], 640*480*3);
cv::imwrite("received.jpg", im);
usleep(1000*10);
}
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <unistd.h>
#include <opencv2/opencv.hpp>
using namespace boost::interprocess;
typedef allocator < unsigned char, managed_shared_memory::segment_manager> shmalloc;
typedef vector< unsigned char, shmalloc > shmimage;
int main( int argc, char* argv[] )
{
shared_memory_object::remove("NAME");
managed_shared_memory shm(open_or_create, "NAME", 640*480*3+1024);
const shmalloc alloc_inst (shm.get_segment_manager());
shmimage * image = shm.construct < shmimage >("Image")(alloc_inst);
shmimage & imref = * image;
imref.resize(640*480*3);
cv::Mat_<cv::Vec3b> im = cv::imread("testsend.jpg");
memcpy(&imref[0], im.data, 640*480*3);
usleep(1000*10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment