10-20-2006 03:11 PM
10-23-2006 07:44 AM
Whilst the CVI library functions are thread safe, they are not always 'thread friendly'. Which is to say they are not reentrant, and to make them thread safe they are protected by Mutex's. Unfortunately, I don't think NI document which groups of functions are not reentrant. (I found this out for myself back in the days of CVI 6. The situation may have changed in later versions of CVI, I don't know).
I would suspect that the CVI function calls in the second thread are locking out the user interface thread. This shouldn't happen, but there appears to be a deficiency in the Windows thread scheduling algorithm that can make this happen in very particular circumstances. I have seen it myself on more than one occasion. Could you try using the Windows API Sleep() function instead of the Delay() function (e.g. Sleep(100) instead of Delay(0.1)) to see if that makes a difference (you will need to #include <windows.h> at the top of the file)?
By the way, if you are using simple variables to communicate between threads, as with stopFlag, you should at least stop the compiler over-optimising by declaring the variable volatile. Better still, use a guaranteed thread-safe communication mechanism.
10-23-2006 01:54 PM
10-23-2006 03:20 PM - edited 10-23-2006 03:20 PM
The short version is declare a variable volatile when one thread is writing to a variable and other threads will read it. You must use thread safe variables when more than one thread can access a value to write to it, and under some conditions when the variable size is larger than the default size of the cpu, for read access as well (for example a 64 bit double on a cpu that uses 32 bit data move instructions).
Volatile simply tells the compiler to use instructions that are less optimized for speed, but not "cache" the variable data to avoid the need for repeated memory access cycles. Thread safe variables actually control the access to a variable to avoid conflicts when two threads try to update the same memory locations, or when a variables memory could be in an undefined state.
Message Edited by mvr on 10-23-2006 03:24 PM