Ask Your Question

pistachio's profile - activity

2016-03-17 15:48:01 -0600 received badge  Nice Question (source)
2016-03-16 00:52:52 -0600 received badge  Student (source)
2016-03-15 16:56:50 -0600 received badge  Editor (source)
2016-03-15 13:26:12 -0600 asked a question Reentrancy problems

Dear OpenCV,

I have version 3.0 on windows 8 embedded (cognex VC5 machine) and I have written 3 threads to process 3 different camera video sequences.

I only call a few openCV functions, the first being cvtColor using BGR2HSV conversion.  I have created a test case with just the three threads and it 'appears' as there is some form of reentrancy issue.  each thread is completely separate, no shared data, and for the test I just create an test matrix (filled with zeros or whatever)

By executing the code on only one thread and not running the other two it runs fine, and when I run all three threads it 'sometimes' works too, but more often causes an assertion.

 It is not specific to the cvtcolor function, however it happens there a lot, I use a few others and they are causing an assertion as well.

As I had to build the openCV for my platform, is there any particular build options I need to set to have the code compile to properly support 'thread safe' results?

Thanks.

Upon examining this it appears that openCV is not re-entrant on initialization. Taking the cvtcolor function for example here is a snippet of code from BGR2HSV case:

    if (is_hsv && depth == CV_8U)
    {
        static UMat sdiv_data;
        static UMat hdiv_data180;
        static UMat hdiv_data256;
        static int sdiv_table[256];
        static int hdiv_table180[256];
        static int hdiv_table256[256];
        static volatile bool initialized180 = false, initialized256 = false;
        volatile bool & initialized = hrange == 180 ? initialized180 : initialized256;

        if (!initialized)
       {

Here they attempt to do a one time initialization of the static tables, but what if thread 2 gets scheduled while thread 1 is processing this one time initialization?

By building protection around my threads I am unable to observe the problem any further...

So if I put a protection around the openCV code in my thread

define oneTimeThreadInit as global initialized to false

if ( myThread == THREAD1 ) || ( oneTimeThreadIniti == true ) ) {

do my openCV code

set oneTimeThreadInit = true }

This is not exact code, as I'd use semaphores.... but the idea is if thread 1 is allowed to run to completion, to execute all my chosen openCV functions, this allows all my openCV functions to execute any one time initialization internally where they are using static vars... after this one time execution of all my chosen openCV functions, then I allow all threads to execute... since I've assured that the initialization is complete inside the openCV functions, I no longer am seeing the assertion error... So without exactly knowing where its conflicting... I believe this is the source of the problem.