简体   繁体   English

如何在boost.interprocess共享内存中使用Complex结构的“ push_back”函数向量

[英]How to use “push_back” function vector of Complex struct in boost.interprocess shared memory

Sorry to bother you. 抱歉打扰你。 I am writing code to make this code," http://coliru.stacked-crooked.com/a/0ab21481b69d87bb " in this question " Structures and vectors in Boost Shared Memory ", the basis 我正在编写代码来编写此代码,“ http://coliru.stacked-crooked.com/a/0ab21481b69d87bb ”是该问题的“ Boost共享内存中的结构和向量 ”的基础

But, I am stuck in compile error of this line. 但是,我陷入了这一行的编译错误中。

data.push_back(id);   // 14 line in main function 

This "data" val is no matching "push_back" function. 此“数据”值与“ push_back”功能不匹配。 This val is declared by "auto&" in this line. 此值在此行中由“ auto&”声明。

auto& data = Shared::locate(smt);   // 10 line in "while(1)" of main function

(This locate(smt) function is construct of a complex struct's vector in shared memory ) (此locate(smt)函数是共享内存中复杂结构的向量的结构)

I assume that "data" is vector or iterator of the complex struct, but there are no way to run this line that I know. 我假设“数据”是复杂结构的向量或迭代器,但是我无法运行此行。

I wonder I was wrong somewhere in this code, but I don't know where is. 我想知道我在此代码的某个地方错了,但是我不知道在哪里。

Please, help me. 请帮我。

Note 1 : line of "data.push_back(id);" 注1 :“ data.push_back(id);”行 is no matching function for call to 'boost::container::vector, boost::interprocess::iset_index> > >, boost::interprocess::allocator, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager, boost::interprocess::iset_index> > >::push_back(InData&)' candidate : void boost::container::vector::push_back(const T&) [with T = BasicInData, boost::interprocess::iset_index> > >; 没有匹配的函数来调用'boost :: container :: vector,boost :: interprocess :: iset_index>>>,boost :: interprocess :: allocator,boost :: interprocess :: iset_index>>,boost :: interprocess :: segment_manager,boost :: interprocess :: iset_index>>> :: push_back(InData&)' 候选对象 :void boost :: container :: vector :: push_back(const T&)[with T = BasicInData,boost :: interprocess :: iset_index>>>; Allocator = boost::interprocess::allocator, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager, boost::interprocess::iset_index> >]| 分配器= boost :: interprocess :: allocator,boost :: interprocess :: iset_index>>>,boost :: interprocess :: segment_manager,boost :: interprocess :: iset_index>>] |

Note 2 : Now, I try to change to the line below; 注意2 :现在,我尝试更改为下面的行;

cv::Mat_<cv::Vec3b> mat;
cv::VideoCapture vcap(0);

InData id(Shared::alloc<uchar_allocator>);
   // camera open check
if (!vcap.isOpened())
    return -1;

But, another compile error occuring "'id' which is of non-class type ". 但是,发生另一个编译错误,该错误是非类类型的“'id'”。 I feel lack of something. 我觉得没有东西。 How do i do? 我该怎么办?

//  opencv's lib    
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
//  opencv's lib end
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>

#include <boost/interprocess/managed_mapped_file.hpp> // use for Coliru
#include <boost/interprocess/containers/vector.hpp>   // boost/containers/vector.hpp
#include <boost/interprocess/containers/string.hpp>   // boost/containers/string.hpp
#include <iostream>
#include <sys/time.h>
#include <stdio.h>

 //                                                                    void_allocator;
namespace bip = boost::interprocess;

typedef unsigned char uchar;
//Typedefs of allocators and containers
typedef bip::managed_shared_memory::segment_manager                       segment_manager_t;
typedef bip::allocator<void, segment_manager_t>  void_allocator;

typedef void_allocator::rebind<uchar>::other                           uchar_allocator;
typedef bip::vector<uchar, uchar_allocator>                                   uchar_vector;



template <typename Alloc = std::allocator<char> >
struct BasicInData {

    public:
        BasicInData(Alloc alloc = {}) : image(alloc)
        { }

        template <typename T>
        BasicInData(double x, int sizeImg, uchar_vector& image, Alloc alloc = {}) :
            x(x), sizeImg(sizeImg), image(alloc)
        { }

        double x = 0;
        int sizeImg = 0;
        uchar_vector image;
};

using InData = BasicInData<>; // just heap allocated

namespace Shared {
    using segment                      = bip::managed_shared_memory;
    using segment_manager              = segment::segment_manager;

    template <typename T> using alloc  = bip::allocator<T, segment_manager>;
    template <typename T> using vector = bip::vector<T, alloc<T> >;

    using InData = BasicInData<alloc<char> >; // shared memory version

    vector<InData>& locate(segment& smt) {
        auto* v = smt.find_or_construct<vector<InData> >("InDataVector")(smt.get_segment_manager());
        assert(v);
        return *v;
    }
}


int main(int argc, char* argv[]) {
    if(argc == 1){ //Parent process
        struct timeval tv;
        // there are making no sense 2 line in below, just examine
        gettimeofday(&tv, NULL);
        double time = ((double)tv.tv_usec/1000000);
        // Remove shared memory on construction and destruction

        // Create a new segment with given name and size
        struct shm_remove
        {
            shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
            ~shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
        }remover;
        Shared::segment smt(bip::create_only,"MySharedMemory", 65536); // 65536 for coliru
        auto& data = Shared::locate(smt);
        //Shared::alloc bip::alloc_inst (data);
        // Camera Capture 
        cv::Mat_<cv::Vec3b> mat;
        cv::VideoCapture vcap(0);

        InData id;
        // camera open check
        if (!vcap.isOpened())
            return -1;

        while (1) { // while keyboard key push
            vcap >> mat; // camera to mat
            int image_size = mat.total() * mat.elemSize();
            id.sizeImg = image_size;
            id.image.resize(image_size * sizeof(uchar));
            memcpy(&id.image[0], mat.data, image_size * sizeof(uchar));
            // get microsecond by double
            gettimeofday(&tv, NULL);
            double time = ((double)tv.tv_usec/1000000);
            id.x = time;
            data.push_back(id);
            if(cv::waitKey(30) >= 0) break; // keyboard pushed
        }
        //Launch child process
        std::string s(argv[0]); s += " child";
        if(0 != std::system(s.c_str()))
            return 1;

        // check child has destroyed the vector
        if(segment.find<vector<InData>>("InDataVector").first)
            return 1;
        }
    }else{
        // Open the managed segment
        managed_shared_memory segment(open_only, "MySharedMemory");

        // Find the vector 
        vector<InData> *myvector = segment.find<vector<InData>>("InDataVector").first;
        // Use vector in reverse order

        vector<InData>::iterator it;

        cv::Mat_<cv::Vec3b> im;
        for(it = myvector->begin(); it !=myvector->end(); ++it){
            im.resize(it->sizeImg);
            memcpy(im.data, &imref[0], it->sizeImg);
            cv::imshow("window1", im);
        }

        segment.destroy<vector<InData>>("InDataVector");

        return 0;
    }
}

Thank you all for helping. 谢谢大家的帮助。 Finally I can get it. 终于我明白了。

In this line in main function is 这行的主要功能是

InData id;

it is changed to 它被更改为

Shared::InData id(smt.get_segment_manager());

This is full code. 这是完整的代码。

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp> // use for Coliru
#include <boost/interprocess/containers/vector.hpp>   // boost/containers/vector.hpp
#include <boost/interprocess/containers/string.hpp>   // boost/containers/string.hpp
#include <iostream>
#include <sys/time.h>
#include <stdio.h>

 //                                                                    void_allocator;
namespace bip = boost::interprocess;

typedef unsigned char uchar;
//Typedefs of allocators and containers
typedef bip::managed_shared_memory::segment_manager                       segment_manager_t;
typedef bip::allocator<void, segment_manager_t>  void_allocator;

typedef void_allocator::rebind<uchar>::other                           uchar_allocator;
typedef bip::vector<uchar, uchar_allocator>                                   uchar_vector;



template <typename Alloc = std::allocator<uchar> >
struct BasicInData {

    public:
        BasicInData(Alloc alloc = {}) : image(alloc)
        { }

        template <typename T>
        BasicInData(double x, int sizeImg, uchar_vector& image, Alloc alloc = {}) :
            x(x), sizeImg(sizeImg), image(alloc)
        { }

        double x = 0;
        int sizeImg = 0;
        uchar_vector image;
};

using InData = BasicInData<>; // just heap allocated

namespace Shared {
    using segment                      = bip::managed_shared_memory;
    using segment_manager              = segment::segment_manager;

    template <typename T> using alloc  = bip::allocator<T, segment_manager>;
    template <typename T> using vector = bip::vector<T, alloc<T> >;

    using InData = BasicInData<alloc<uchar> >; // shared memory version

    vector<InData>& locate(segment& smt) {
        auto* v = smt.find_or_construct<vector<InData> >("InDataVector")(smt.get_segment_manager());
        assert(v);
        return *v;
    }
}


int main(int argc, char* argv[]) {
    if(argc == 1){ //Parent process
        // Remove shared memory on construction and destruction

        // Create a new segment with given name and size
        struct timeval tv;
        gettimeofday(&tv, NULL);
        struct shm_remove
        {
            shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
            ~shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
        }remover;
        Shared::segment smt(bip::create_only,"MySharedMemory", 65536); // 10 Kb for coliru
        auto &data = Shared::locate(smt);
        //Shared::alloc bip::alloc_inst (data);

        cv::Mat_<cv::Vec3b> mat;
        cv::VideoCapture vcap(0);

        Shared::InData id(smt.get_segment_manager());


        if (!vcap.isOpened())
            return -1;

        while (1) {
            vcap >> mat;
            int image_size = mat.total() * mat.elemSize();
            id.sizeImg = image_size;
            id.image.resize(image_size * sizeof(uchar));
            memcpy(&id.image[0], mat.data, image_size * sizeof(uchar));
            //Launch child process
            gettimeofday(&tv, NULL);
            double time = ((double)tv.tv_usec/1000000);
            id.x = time;
            data.push_back(id);
            if(cv::waitKey(30) >= 0) break;
        }

        std::string s(argv[0]); s += " child";
        if(0 != std::system(s.c_str()))
            return 1;

        // check child has destroyed the vector
        if(smt.find<Shared::vector<InData>>("InDataVector").first)
            return 1;

    } else{
        // Open the managed segment
        bip::managed_shared_memory segment(bip::open_only, "MySharedMemory");

        // Find the vector using c-string name
        bip::vector<InData> *myvector = segment.find<bip::vector<InData>>("InDataVector").first;
        // Use vector in reverse order

        bip::vector<InData>::iterator it;

        cv::Mat_<cv::Vec3b> im;
        for(it = myvector->begin(); it !=myvector->end(); ++it){
            im.resize(it->sizeImg);
            memcpy(im.data, &it->image[0], it->sizeImg);
            cv::imshow("window1", im);
        }

        segment.destroy<bip::vector<InData>>("InDataVector");

        return 0;
    }
}

我建议获取BasicInData(const BasicInData &data)BasicInData &operate =(const BasicInData &data)函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM