LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Loop not stopping

Dear all,

i have written a small program using loop parallelism.

In one loop, i am using enqueue function and in the other loop i am using dequeue function. And i am using a single boolean to control both the loops.

However, when i stop the put the boolean to true, the loop doesnot terniminate.

 

One reason could be that the deque function waits for infinite time for an element to enter queue. But if that is the case, what is the use of using single boolean(as a local variable) to control multiple loops??

 

I am posting my VI here .. pls have a lok at it .

 

Thanks,

Ritesh

0 Kudos
Message 1 of 13
(4,014 Views)

Hi Ritesh,

you don´t need a local variable. Conenct the error output from the dequeue element to the loop condition. If you destroy the queue, you will receive an error and the other loop stos.

See the attached example.

 

Mike

Message 2 of 13
(4,011 Views)

Hi Mike,

I got your solution. But that will be on of the solutions.

I want to know why local variable is not able to termiante the 2nd loop?

 

Thanks,

Ritesh

0 Kudos
Message 3 of 13
(4,006 Views)

Ritesh,

 

you issue has a simple reason....

Do not use event based programming (queues) and variables mixed in such a way. Take a look into the design patterns delivered with LabVIEW. All of the parallel-loop-architectures use

a) proper error handling

b) the error handling to stop the second loop

 

The reason for your hang is a classic race condition. You read "True" in the consumer when the producer is already exited, so you will never send a queueelement anymore but the consumer is still waiting for another element! If you send one additional element AFTER the producer, the consumer will stop working too. But please go for Mikes' example because this is the more valuable....

 

hope this helps,

Norbert 

 

[Edit]: If you'd insert the local variable with a single sequence step and synchronize it using the errorcluster to take place after the dequeue within your consumer, it will work as well. But please note that this is ugly style!

Message Edited by Norbert B on 09-11-2008 06:27 AM
Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 4 of 13
(4,004 Views)

Norbert,

Yes is due to the happening of Race Condition.

But, when i run my program with highlight execution ON, the program terminates properly. And the TRUE condition of the variable on the both the loops occur simultaneously.

Only when i run my program with the highlight ececution OFF, it goes into infinite loop. What could be the reason for that ?

 

Thanks,

Ritesh

 

0 Kudos
Message 5 of 13
(3,984 Views)

ritesh024 wrote:
[...] And the TRUE condition of the variable on the both the loops occur simultaneously.[...]

 


That is definetly wrong. And that is why NI recommends all developers not to use variables if not really, really, really neccessary!

 

The consumer loop copies the value of stop when it enters its iteration. This copied value remains until the next iteration. So if you enter the loop at some time (producer and consumer will never likely start their iteration without any delay in the ps-range!), you will either get the "new" value or the "old".

And take it as it is: if you force the execution to slow down, you are near 100% to read the "new" value so the consumer is exited. Without this forcing, you are near 100% to read the "old" value leading to the observed behaviour. My remark in [Edit] shows what is real going on and you have to implement a better solution: one without variable!

 

hope this helps,

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 6 of 13
(3,981 Views)

Norbert,

Thanks for your detailed reply.

I am geting each bit of it.

Theoretically everything seems to be perfectly fine. But, practically things are looking different.

 I am attaching a piece of code here. 

Now, in both the loops i am reading the values of the variable. both of them shows TRUE, and both the loops terminates. still program keeps running

 

Just have a look at the code and see if my interpretation is right.

 

Thanks,

Ritesh

0 Kudos
Message 7 of 13
(3,972 Views)

Hi Ritesh,

that´s what Norbert described in the last posts. Only the part which reads the local variable execute, but the dequeue function still wait for an event. You can use the highlight function to check it, but then it could be that it work´s. You can also use probes and break points to test it. Try to use some debugging tools. Smiley Wink

 

Mike

Message Edited by MikeS81 on 09-11-2008 03:20 PM
Message 8 of 13
(3,953 Views)

Yes,

debugging did Help.

But why is that when the order for termination is give, the deque function still waits for an input.

I didnt get the scope of this.

I feel termination of the loop should be given the highest priority, and not the deque function as is it occuring inside the loop.

But things aren't that way.

 

Ritesh

0 Kudos
Message 9 of 13
(3,946 Views)

Ritesh,

 

i don't understand what there is to discuss.

You have a race condition in your code. A race condition CAN lead to unexpected behaviour of any kind, but not neccessarily (if you are lucky).

Your reaction feels a bit like: "Hey, i made a mistake, but since i don't get the point WHY i made the mistake, another one has to make sure that my race condition never affects my code in a negative way..... "

 

Please make it a rule: Never use variables of any kind since they are prone to race conditions!

There are some exceptions to this rule for sure, but  keep it as a guiding light for proper coding methods.....

 

hope this helps,

Norbert

 

P.S.: Since i am confident that sufficient workarounds have been shown in the last posts, i will not give any other workarounds..... 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 10 of 13
(3,934 Views)