I know, guys, there is s***t load of posts about std::async, but I just cant figure it out myself (for my defense I can say that today was my first day looking at whole multithread & async thing...).
Here's what I have and what I want it to do - I have a camera and a projector and I want to synchronize them. They are connected so the projector signal triggers acquisition of images. HOWEVER I have to call for acquisiton start before I start displaying images via projector. I am using OpenCV as image processing/handling lib.
class Camera
{
public::
//this one captures images till vector reaches given count of images (while loop)
std::vector<cv::Mat> captureSetOfFrames(uint count = 10)
{
std::vector<cv::Mat> vect;
while(vect.size() < count) { /*try to capture and append to vect */ };
}
};
class Projector
{
public:
void start();
void stop();
}
int main()
{
Projector *proj = new Projector();
Camera *cam = new Camera();
std::vector<cv::Mat> recvimgs;
recvimgs.reserve(10);
auto f = std::async(&Camera::captureSetOfFrames, cam, PATTERN_10);
proj->start();
while(recvimgs.size() < 10)
recvimgs = f.get();
proj->stop();
}
I get an error:
3 IntelliSense: no operator "=" matches these operands
operand types are: std::vector<cv::Mat, std::allocator<cv::Mat>> = std::vector<cv::Mat, std::allocator<cv::Mat>> (unsigned short)
- From where this
unsigned shortat the end of error is coming from...? - How can I achive this behaviour? (If the code example is not clear enough I want first to prepare for grabbing by calling
Camera::captureSetOfFrames()then simultaneously callProjector::start()but knowing camera is ready for capturing, finally terminate displaying by callingProjector::stop()when all images are captured)
Hope you can figure it out!
PS: Base post which suggested me this (not working) solution How to, in C++11, use std::async on a member function?
captureSetOfImagesandcaptureSetOfFramestwo functions or a typo? (Also, don't rely too much on IntelliSense – it's just an approximation.)get()can be called once on afuture.getreturns the entire result the first time you call it (it waits for the computation to finish if it hasn't) and any further calls are undefined.captureSetOfFrameshas been called just becausestd::asyncreturns – all you know is that the function will have been called beforegetreturns. If you use the overload you're using it may even be called synchronously whengetis called, depending on the implementation.captureSetOfImagesandcaptureSetOfFramesis a typo, im gonna fix it in a moment. So basicly you say that it can be done like:auto f = std::async(&Camera::captureSetOfFrames, cam, 10); proj->start(); recvimgs = f.get(); proj->stop();Couse i assume from what you say thatcaptureSetOfFrameswill be running when istart()and it will wait tillf.get()return a value next?