Problem in passing function to QtConcurrent
-
wrote on 13 Mar 2018, 09:35 last edited by VRonin
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.
-
wrote on 13 Mar 2018, 09:54 last edited by VRonin
- Why, in 2018, in C++, would you use
char[]
to hold a string? UseQString
orstd::string
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).- 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 keepstr
alive untilrun()
finishes - You are passing
Mat
around by value. If it's not implicitly shared (or you can force somestd::move
magic) that might be fairly expensive in terms of resources
- Why, in 2018, in C++, would you use
-
- Why, in 2018, in C++, would you use
char[]
to hold a string? UseQString
orstd::string
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).- 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 keepstr
alive untilrun()
finishes - You are passing
Mat
around by value. If it's not implicitly shared (or you can force somestd::move
magic) that might be fairly expensive in terms of resources
- Why, in 2018, in C++, would you use
-
wrote on 13 Mar 2018, 10:34 last edited by VRonin
@Kira said in Problem in passing function to QtConcurrent:
@VRonin : Can you please elaborate point 4
I mean, every time you callsaveOperation
you are triggering a copy ofMat
. 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 copyEDIT:
I just noticed
Mat
iscv::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
-
@Kira said in Problem in passing function to QtConcurrent:
@VRonin : Can you please elaborate point 4
I mean, every time you callsaveOperation
you are triggering a copy ofMat
. 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 copyEDIT:
I just noticed
Mat
iscv::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
1/5