cv::Mat Pointers, Scope and Threading

asked 2014-09-15 21:05:41 -0600

MRDaniel gravatar image

Hello,

I would like to know about smart pointers and thread safety. If we take a simple example we create an image and pass it to two (hypothetical) 'threaded' functions. They use the cv::Mat, not a cv::Mat* or cv::Mat&, the idea being that when they go out of scope, they will be automatically released.

void functionX(cv::Mat image)
{
    //Do some threading things
    //Async long running.
}

void functionY(cv::Mat image)
{
    //Do some other threading things
    //Async long running.
}

main()
{
    cv::Mat img = cv::imread('someImage.png',1);

    functionX(img);
    functionY(img);

}

Will the cv::Mat deallocate itself? I have read about smart pointers and they can make the object they are pointing to thread safe. With the std::shared_ptr() it is thread safe so long as two threads try not to write to the same shared_ptr() object, instead of two separate shared_ptrs to the base object.

Is this the case for OpenCV's cv::Mat?

When we call functionX or functionY, with the above call signatures, is a new cv::Mat header created for each of those functions? Headers that reference the same memory for the object img?

Will they be garbage collected when it goes out of scope?

Regards,

Daniel

edit retag flag offensive close merge delete

Comments

@StevenPuttemans is the guy to ask here me thinks. Any thoughts buddy?

MRDaniel gravatar imageMRDaniel ( 2014-09-15 21:09:14 -0600 )edit

Hmm not quite sure but afaik there is no thread safety for Mat objects and one should make sure that he uses a mutex structure to avoid two threads reading the same Mat which is essentially a pointer structure.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-09-16 02:35:42 -0600 )edit