【问题标题】:How to initialize objects with threads in cpp? Is it feasible如何在cpp中使用线程初始化对象?是否可行
【发布时间】:2022-01-19 10:57:06
【问题描述】:

我想初始化两个 Collage 对象,通过线程创建它们是否可行,如果可以,如何? 我想创建两个拼贴画,然后在不同的线程中分别对它们调用 2-stitch,最后将它们修改后的图像组合到一个新的图像矢量中,形成一个新的拼贴画。 这是我的.h 拼贴文件


#include <iostream>
#include <stdarg.h>

namespace img {

    class Collage {
            public:
                Collage( vector<Image> imageArr);
                ~ Collage();

                //create an array of coordinates of corners of each of the images, variable
//                int subImageCorners[numImages * 4];

                void twoStitch(bool );
                void threeStitch();
                void fourStitch(bool );
                void fourStitchRec(int times);
                void flip();

                const std::vector<int>& testVect();
                int getNumImages();
                const std::vector<double>& getRatios();
                const std::vector<Image>& getImageArr();
                Mat getModifiedImage();
                void setModifiedImageArr(vector<Image> imageArrModified);
                vector<Image> getModifiedImageArr();
                void setModifiedImage(Mat modifiedMat);
                const std::vector<double>& getModifiedRatios();

                void setFourStitchRecImgArr(Mat image);
                const vector<Image>& getFourStitchRecImgArr();
            private:
                vector <Image> fourStitchRecImgArr;
                int numImages;
                int modifiedNumImages;
                vector <double> ratios;
                vector <double> modifiedRatios;
                vector <Image> imageArr;
                Mat modifiedImage;
                vector <Image> imageArrModified;
                void fourStitchRecAux(bool, int times);
    };
}

我尝试只调用线程中的成员函数,如下所示:

    if (this->getNumImages() == 4) {
        vector<double> ratios;
        vector<Image> subImageArr1;
        if (original){
            ratios = this-> getRatios();
            subImageArr1 = this->getImageArr();
        } else{
            ratios = this-> getModifiedRatios();
            subImageArr1 = this->getModifiedImageArr();
        }

        int maximum = getMaxIndex(ratios);
        // std::cout << "maximum: " << typeid(maximum).name();

        vector<Image> subImageArr2;
        //split into two collages of double stitch and then stitch all of them together
        vector<Image>::iterator maxIndex = subImageArr1.begin() + maximum ;
        vector<double>::iterator maxRatiosIndex = ratios.begin() + maximum ;
        subImageArr2.push_back(*maxIndex);
        ratios.erase(maxRatiosIndex);
        subImageArr1.erase(maxIndex);
        int secondMaximum = getMaxIndex(ratios);
        vector<Image>::iterator secondMaxIndex = subImageArr1.begin() + secondMaximum ;
        subImageArr1.erase(secondMaxIndex);
        subImageArr2.push_back(*secondMaxIndex);
        Collage subCollage1(subImageArr1);
        Collage subCollage2(subImageArr2);
        std::thread th1(&Collage::twoStitch, subCollage1);
        std::thread th2(&Collage::twoStitch, subCollage2);
        th1.join();
        th2.join();
        vector<Image> imageArrModified ={subCollage1.getModifiedImage(), subCollage2.getModifiedImage()};
        this->setModifiedImageArr(imageArrModified);
        this->twoStitch(false);



    } else{
        std::cout << "A different amount of images than 4!";
    }

}```

And I get the errors bellow:
> /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/thread:280:5: error: attempt to use a deleted function
>    __invoke(_VSTD::move(_VSTD::get<1>(__t)), >_VSTD::move(_VSTD::get<_Indices>(__t))...);
>    ^
>/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/thread:291:5: note: in instantiation of function template >specialization >'std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_s>truct, void (img::Collage::*)(bool), img::Collage, 2>' requested >here
>    __thread_execute(*__p, _Index());
>    ^
>/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/thread:307:47: note: in instantiation of function template >specialization >'std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::_>_1::__thread_struct>, void (img::Collage::*)(bool), img::Collage>>' >requested here
>    int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, >__p.get());
>                                              ^
>/Users/yao/CLionProjects/video_editor_BX23/src/image/test_image_class >/../collage/collage.cpp:199:21: note: in instantiation of function >template specialization 'std::__1::thread::thread<void >(img::Collage::*)(bool), img::Collage &, void>' requested here
>        std::thread th1(&Collage::twoStitch, subCollage1);
>                    ^
>/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/type_traits:1967:5: note: '~__nat' has been explicitly marked >deleted here
>    ~__nat() = delete;
>    ^

【问题讨论】:

  • 是的,这是可能的。但是不要看 std::thread,而是看一下 std::async。它比 std:: 线程(返回值 + 异常)有一些好处,并且是 imo 更好的抽象。

标签: c++ multithreading


【解决方案1】:

您似乎正试图在实例 subCollage1subCollage2 上调用成员函数 Collage::twoStitch - 但 Collage::twoStitch 需要一个您未提供的 bool 参数。此外,要调用成员函数,您需要提供指向实例的指针。

例子:

#include <iostream>
#include <thread>

struct Collage {
    void twoStitch(bool b) {
        std::cout << b << '\n';
    }
};

int main() {
    Collage subCollage1;
    
    // 1. pointer to member function
    // 2. pointer to instance
    // 3. the argument(s) the function requires
    std::thread th1(&Collage::twoStitch, &subCollage1, true);
    
    th1.join();
}

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2013-06-27
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多