Problem in passing function to QtConcurrent



  • I have declared an structure as:
    struct SaveImage{ char fileName[100]; Mat image; };
    and then qqueue of as: QQueue<SaveImage> saveQueue;

    In-one function i am declaring the object of the queue and passing the value to the queue and calling the qtconcurrent function as:

    fun A()
    {
         SaveImage str;  //Object of the queue
                 str = saveQueue.takeFirst();
               future = QtConcurrent::run(this,&MainWindow::saveOpeartion, &str.fileName, str.image);
    }
    
    
    // Given below is the save operation function:
    //void MainWindow::saveOperation(char fileName[], Mat capturedFrame)
    {
    }
    

    I am getting the following error:

    'QtConcurrent::VoidStoredMemberFunctionPointerCall2<T,Class,Param1,Arg1,Param2,Arg2>::arg1': array initialization requires a brace-enclosed initializer list
    with
    [
        T=void,
        Class=MainWindow,
        Param1=char [],
        Arg1=char [100],
        Param2=cv::Mat,
        Arg2=cv::Mat
    ]
    

    Just need to know the approach of declaring making qqueue of struct is correct or not or how we can achieve the above mentioned implementation.



    1. Why, in 2018, in C++, would you use char[] to hold a string? Use QString or std::string
    2. QtConcurrent on a member function is VERY dangerous. You have to make sure there are no race conditions manually (including making sure the object doesn't get deleted before run finishes).
    3. You can still do what you want using QtConcurrent::run(std::bind(&MainWindow::saveOpeartion,this,str.fileName,str.image)); but, if you don't use a proper string class as I suggested above, you have to find a way to keep str alive until run() finishes
    4. You are passing Mat around by value. If it's not implicitly shared (or you can force some std::move magic) that might be fairly expensive in terms of resources


  • @VRonin : Can you please elaborate point 4



  • @Kira said in Problem in passing function to QtConcurrent:

    @VRonin : Can you please elaborate point 4

    I mean, every time you call saveOperation you are triggering a copy of Mat. Since I assume that's an image, if it does not have cheap copy constructors like Qt does it might take a long time to do the copy

    EDIT:

    I just noticed Mat is cv::Mat. The copy constructor is cheap but you have to remember that, from https://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html#a294eaf8a95d2f9c7be19ff594d06278e:

    when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m

    So you are not really copying it at all



  • @VRonin : thanks have implemented the above logic using string instead of character arrary
    Will also keep in mind the instructions for cv::Mat


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.