Ask Your Question

is cv::Mat ref counter atomic?

asked 2018-04-23 23:54:40 -0500

hovnatan gravatar image

I have global variable cv::Mat a, then I'm trying to read access two different parts of a from 2 different threads using ROI indexing: cv::Rect r; const cv::Mat tile = a(r);. Will this type of access create race conditions if I only do read on tile? In particular, the ref counter in a will increment with each such access, is this counter an atomic variable, won't it create a race condition?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-24 01:05:57 -0500

berak gravatar image

yes, the internal refcounter is atomic, and yes, a ROI operation will increase it.

unfortunately, this does not mean, that opencv can be used generally in a threadsafe way, a lot of functions / classes can NOT be used with multithreading, try to avoid it, and rather enable internal parallelism (TBB,OMP, etc.) at build time.

edit flag offensive delete link more


@berak thanks! If I need to tile based processing of an image (independent from each other), what is the best way to proceed?

hovnatan gravatar imagehovnatan ( 2018-04-24 01:11:58 -0500 )edit

sorry, not enough information to answer. (apart from the usual: profile it before attempting to throw multithreading at it)

berak gravatar imageberak ( 2018-04-24 01:19:15 -0500 )edit

@berak what if I use TBB based functions on two completely different images in two different threads? Is that threadsafe?

hovnatan gravatar imagehovnatan ( 2018-04-25 00:49:24 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-04-23 23:54:40 -0500

Seen: 221 times

Last updated: Apr 24 '18