1

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)   
  1. From where this unsigned short at the end of error is coming from...?
  2. 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 call Projector::start() but knowing camera is ready for capturing, finally terminate displaying by calling Projector::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?

Community
  • 1
  • 1
michelson
  • 686
  • 9
  • 22
  • Are `captureSetOfImages` and `captureSetOfFrames` two functions or a typo? (Also, don't rely too much on IntelliSense – it's just an approximation.) – molbdnilo Jan 27 '16 at 23:37
  • Also, this doesn't make much sense. `get()` can be called once on a `future`. – T.C. Jan 28 '16 at 00:03
  • A future does not deliver partial results. `get` returns 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. – molbdnilo Jan 28 '16 at 00:57
  • Also, you don't know that `captureSetOfFrames` has been called just because `std::async` returns – all you know is that the function will have been called before `get` returns. If you use the overload you're using it may even be called synchronously when `get` is called, depending on the implementation. – molbdnilo Jan 28 '16 at 01:08
  • @molbdnilo Sorry of course `captureSetOfImages` and `captureSetOfFrames` is 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 that `captureSetOfFrames` will be running when i `start()` and it will wait till `f.get()` return a value next? – michelson Jan 28 '16 at 07:02

0 Answers0