depends.
if that is allowed to take a few seconds, open the VideoCapture, read a frame, close it again.
if that is not allowed to take a few seconds, then you’ll have to work with threads.
tutorial from the official docs: OpenCV: Getting Started with Videos
older recipe of mine for thread stuff: freshest_camera_frame.py · GitHub
I’d recommend using Python for all of this. C++ is just pain. Python will not take performance from you. All the library calls go to compiled code.