'Asynchronous reading and recording videos using opencv

I am struggling with a problem: I need to connect to almost 100 IP Cameras and record their stream in separate files using OpenCV. The process of connecting and reading is fine. However, while I try to write frames using cv::VideoWriter, it takes time. In another word, reading and writing is not synchronized.

I tried to buffer images, and write them in a separate thread! But I faced two more problems: 1) there should be a huge memory, 2) and, experimentally, I found out that I need two buffers. The second one is needed to buffer new frames while consuming (writing to file) the first one. Has anyone faced the same problem? Any help or a suggestion would be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source