7

I want to use OpenCV in order to record a video and send it as a stream. I'm a beginner and I need to know if OpenCV blocks the main thread or is it threaded itself ?

I read OpenCV documentation (2.4.9) and I couldn't find any answer.

Thanks for reading.

1
  • It is blocking. At least all functions that I know. Commented Oct 10, 2014 at 19:11

3 Answers 3

6

OpenCV can spawn threads when you call a function. However, all the work is performed before control is returned to the calling thread. For a number of reasons, asynchronous processing would add a substantial extra bit of complexity. (Consider, for instance: How would your program know when the computation was done?) It would also introduce some undesired overhead if the program didn't need to be asynchronous.

You can do asynchronous processing yourself with a minimal amount of effort, though, with C++11's threading API.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it may spawn threads. OpenCV can use multiple threads to process different chunks of data ("parallel regions") in parallel. See setNumThreads and nearby functions for details.
4

OpenCV can be built with OpenMP support to make the compute functions use all the available cores on your machine. It can be built also with OpenCL and CUDA. In addition it has sorts of SIMD optimization flags.

If you don't build it with such support it will run single threaded.

In either versions, calling an OpenCV function blocks the launcher thread until it computes all the operations. That is true even when offloading the computation to a GPU.

1 Comment

Despite the title, i think the main question was whether "OpenCV blocks the main thread"
0

OpenCV's parallel_for operations creates multiple threads to operate on. It builds a thread pool and distributes the work across it.

The number of threads is decided by setNumThreads function. Set it to zero for serial work. [ This changes based on the threading library that opencv is build with with. For some it is 1 ]

Note : I had some threading issues where I was managing my own threadpool.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.