Thread lock around avcodec_open / close

I have C ++ code - cli that captures video from a folder in opencv using capture and then retrieves frames using a cvquery frame. Then I process the frames, and as soon as all the frames are processed, I release the capture. It works fine, but when I try multithreaded, it gives me a warning and cannot capture some of the videos in the folder with the warning "insufficient blocking of the stream around avcodec_open / close ()".

//for each video in folder do
{
    capture=cvCreateFileCapture(filename);

    while(1)
    {
        img=cvqueryframe(capture) 

        if !img break;
        ///process img
    }

    cvreleasecapture(&capture);
}

Is there a way to fix the problem for multithreading? I was thinking about using

while(!capture) 
    capture=cvCreateFileCapture(filename);

but there should be a more efficient way, perhaps using Monitor :: Enter (obj) or lock (obj) lock?

+5
source share
1 answer

open close avcodec . , , , , , " ".

, cvCreateFileCapture cvreleasecapture (, , avcodec_open avcodec_close), , . , mutex Mutex, - :

extern Mutex m; // application-wide mutex

//for each video in folder do
{
    m.lock();
    capture=cvCreateFileCapture(filename);
    m.unlock();

    while(1)
    {
        img=cvqueryframe(capture) 

        if !img break;
        ///process img
    }

    m.lock();
    cvreleasecapture(&capture);
    m.unlock();
}

Mutex. Linux OS X mutexes pthread. Windows Win32 mutexes.

+10

All Articles