Reputation:
I am having an issue that is most likely related to threads or mutex:
When I call a custom method directly in the main method, it works and gives results. However, the calculation takes ~ 900 ms. So I wanted to make a single thread that only does this calculations and shows the results afterwards.
The project is quite big at this time, I try to give pseudo code that is as close as possible to the original one.
More background information: I stream a depth image from a camera and want to use a thinning algorithm on this image. Using OpenCV 3.1.0 and MSVC 2015 for this (all 32 bit).
int main()
{
//init camera, read images etc...
Mat copyOfImage = dephtImage->clone();
applyThreadedThinning(©OfImage);
return 0;
}
This version works fine. If I call the method instead like that:
std::thread t1(&applyThreadedThinning,©OfImage);
it says "Debug Error!
Program: .... *.exe
abort() has been called
(Press Retry to debug the application)"
I can not imagine why it works without a thread, but with a thread it does not. Debugging is a bit complicated, I just follow every line with "step into" and wait until it makes this error message. Once I isolated the line of code that is responsible for this error, what do I do then?
Upvotes: 0
Views: 509
Reputation:
Solved the abort() problem. The issue seems to be related to scopes/smart pointers. Simply replace every cv::Mat* with cv::Mat (no pointers) and adapt the code for the changed type.
For my case that was a solution. Thanks for the explaining @Hayt, it did really speed up my process.
Upvotes: 0
Reputation: 5370
A c++ std::thread object needs to know what it should do in case it gets destroyed (goes out of scope).
There are 2 options: join
which waits for the thread to finish or detach
to detach the thread from the object.
Because both options are valid it was decided that none of the both of them will be implicit on destruction and an assert (abort) will be thrown when you destroy a std::thread object without calling one of those functions first.
If you want to wait for the thread to finish call t1.join()
before the object goes out of scope.
If you want to detach yourself from the thread and let it finish at some point you don't care about call t1.detach()
before the object goes out of scope.
Upvotes: 1
Reputation: 1191
You should wait in the main function until the thread finishes.
...
t1.join();
return 0;
Upvotes: 0