I am developing a program which is continously receiving frames from a video stream and computing a Motion Estimated Value between each pair of frames.
Due to hardware limitations, I have to compute the Motion Estimation (ME) algorithm in CPU, something which takes about 2 seconds per computation. Because of that, I want to implement the ME algorithm with multithreading. The idea would be to receive the next frames from the stream in a main thread while the Motion Value is being calculated in other thread.
I have done it using one thread per each task, that is, every time a pair of frames is received I created a new thread for compute the Motion Value. However, due to the time elapsed in Motion Computation, a lot of threads are created and run concurrently, which I suppose it's not very efficient.
I think the best way to reimplement this is by using a thread pool. For example, in one hand having a main thread which receives the frames and store them in a buffer or queue and on the other hand having 4 or 8 threads running concurrently and reading from the reception buffer, which if I am not wrong should be protected by mutex. However, the main thread would be receiving frame much faster than one motion computation ending and I don't know how to manage that.
I am very new to C++ and new to threads, so I'd appreciate it if you can provide me some solution in pseudocode just to start my reimplementation.
Thank you so much