0

I am trying to mess around with multi-threading and OpenCV so I wrote the following program.

void performSearch(Mat& original, Mat& grey, Mat& result)
{
    for(size_t index = 0; index < container.size(); ++index)
    {
         // do stuff

         imshow("Results", result);
         waitKey();
    }
}

int main()
{
    thread(performSearch, ref(image), ref(image_grey), ref(answer)).join();
    thread(performSearch, ref(image2), ref(image2_grey), ref(answer2)).join();
}

This program will throw an NSInternalInconsistencyException exception because waitKey() needs to be run on the main thread. My question is, how do I attach it to the main thread?

I have an iOS background and with grand central dispatch, I'd simply have to do something along the lines of:

DispatchQueue.main.async // this gets me the main queue asynchronously
{
    // update UI
}

Is it possible to achieve this in C++? If yes, how?

Heads Up: I have looked at this question and they're using QT for their GUI. I'm not.

eshirima
  • 3,837
  • 5
  • 37
  • 61
  • Your `main` is running on the main thread. However all it does now is sit there waiting for the thread to finish. | What is it you're actually trying to achieve? – Dan Mašek Aug 08 '17 at 17:29
  • @DanMašek I need to run `performSearch` on two different images simultaneously and step through each iteration's result. See my updated question – eshirima Aug 08 '17 at 17:36
  • Actually, after posting my answer and before your edit, I am not quite sure what the purpose of waitKey is inside your loop. – Pruthvikar Aug 08 '17 at 17:39
  • @Pruthvikar `waitKey()` allows me to visualize the results of my image operations after each iteration. Without it, it'd just run all `container.size()` iterations at once without visualizing them. I have `imshow()` calls in there as well. – eshirima Aug 08 '17 at 17:42
  • @DanMašek What would u recommend I do then to achieve my task? `performSearch` needs to be run on both images simultaneously. – eshirima Aug 08 '17 at 17:43
  • Something [like this](https://pastebin.com/urytJJJi)? Or you could even use `std::async` instead, I suppose. – Dan Mašek Aug 08 '17 at 17:50
  • Makes a bit more sense now. One option would be to use shared memory as shown in this answer https://stackoverflow.com/questions/20595760/c-communication-between-threads So you would poll for available results as they get added to a shared vector and then display them. That way the search carries on whilst waiting to show results. – Pruthvikar Aug 08 '17 at 17:50
  • @Pruthvikar Yeah, but it seems to me OP wants to display and wait, before proceeding to search 2 more images in parallel. Otherwise I'd say use some synchronized queues [like here](https://stackoverflow.com/questions/37140643/how-to-save-two-cameras-data-but-not-influence-their-picture-acquire-speed/37146523#37146523) (of course with some modifications) – Dan Mašek Aug 08 '17 at 17:52
  • Ah, OP can you confirm if you want the search to pause when a result is found? Doesn't seem like that would make sense – Pruthvikar Aug 08 '17 at 17:53
  • I do want to pause when the result is found. That's why I kept the `waitKey()` inside the for loop. – eshirima Aug 08 '17 at 17:54
  • 2
    use a "conditional wait" in the main thread that waits until your thread has perforned the imshow and a conditional wait in the worker thread that waits until the waitKey (that's executed in the main thread after the conditional wait) has finished. – Micka Aug 08 '17 at 20:28

0 Answers0