I have a c++-cli code that is capturing videos from a folder in opencv using capture and then retrieving frames using cvquery frame. I then process frames and once all frames are processed i release the capture. It works fine but when I try to multithread it gives me a warning and cannot capture some of the videos in the folder with warning " insufficient thread locking 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 of using
while(!capture)
capture=cvCreateFileCapture(filename);
but there should a more efficient way, maybe using locking Monitor::Enter(obj) or lock(obj)?
The open
and close
functions in avcodec are not thread safe. To prevent issues in multi-threaded apps they have a simple mechanism that detects when two threads are inside these functions at the same time and when that happens they write the "insufficient thread locking" message.
The way to prevent this message is to lock the calls to cvCreateFileCapture
and cvreleasecapture
(which in turn call avcodec_open
and avcodec_close
) to ensure that these calls are always serialized. For example, if you had a mutex class called Mutex you could do something like this:
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();
}
There are many Mutex implementations. On Linux or OS X you can use pthread mutexes. On Windows you can use Win32 mutexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With