11-29-2012 07:38 PM - edited 11-29-2012 08:05 PM
I'm a new bie to LabVIEW.
I have a question about local variable in LabView.
I tried to stop a for loop by local variable but the value of loop counter is different with my expected value.
I think the loop to stop at 6 but it stopped at 7.
Could anyone help me?
Solved! Go to Solution.
11-29-2012 08:41 PM
That sounds exactly like a race condition. You have know control over whether the terminal of the control is read first or the local variable is written to first.
Try running your code with Highlight Execution on.
In your code what will usually happen is that the control terminal is read and the value is false. Then other code will execute allowing the value to be written to the local variable. Let's say in a particular loop iteration, it is gets set to True. But the control terminal value has already been read as false, so the loop iterates again. Next iteration the control terminal is read and then it sees the True and will force the loop to stop no matter what happens in the rest of the loop.
So, why are you using a local variable instead of just sending the wire to the stop terminal?
11-29-2012 09:05 PM
Thanks for your quick answer.
I have tried Highlight Execution and found the control terminal was read first and written later by local variable as you said.
There is no special reason for using a local variable instead of just sending the wire to the stop terminal.
It's just because I'm a new bie to LabVIew and think using local varible will have the same result.
Thank you very much.
11-29-2012 10:50 PM
Since you are new to LabVIEW, make sure you take some tutorials if you haven't already done so.
LabVIEW Introduction Course - Three Hours
LabVIEW Introduction Course - Six Hours
For more information about local variables read Why some people say Local Variables are bad...
11-29-2012 10:56 PM
LabVIEW is a dataflow language and as such a wire is not the same as local variables. LabVIEW also supports parallel processing which is a benefit of dataflow. Essentially, any node (primitive such as an add, a structure such as a case statement or loop, or a subVI as well as others) can be executed as soon as all of its inputs have been satisfied. Local variables break dataflow by allowing a single item to be accessed at the same time in two places at the same time. Local variables also creates copies of the data where a single wire will not. When writing code in LabVIEW you should really try to learn and understand data flow and use it within your code.