LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Two while loops in parallel - 2nd isn't executed

Solved!
Go to solution

Hello,

 

In this simple setup there are 2 while loops:

  • 1st - for setup
  • 2nd - operation, where X variable increments

The problem is: after 1st run variables X isn't reset to X0, so VI stops immediately after it starts.

The reason - 2nd loop isn't executed, so X0 value isn't propagated to X.

 

Thanks in advance

 

Pavel

 

Two while loops in parallel - 2nd isn't executed.JPG

0 Kudos
Message 1 of 15
(5,981 Views)

Hi Pavel,

 

simple answer: RACE CONDITION due to local variables…

 

Your "loop condition" is TRUE from last run of your VI. When you start your VI the next time the lower while loop seems to read the variable faster than it is written to in the upper loop. RACE CONDITION!

 

To transfer data from one loop to the other you should think about using notifiers or queues…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 15
(5,970 Views)

Yesterday, you posed this same question under a different topic, and received several suggestions about how to accomplish what you seem to want.  In particular, I posted a single-loop simple method that did most of what you seem to want, and which you should be able to modify to suit your purposes.  Most of the solutions had single loops, none of them had local variables.

 

When you post a question on the Forum, do you actually try the code that is suggested?  Do you run it and see if it works?  If you don't understand how it works, do you turn on Execution Highlighting (the little light bulb on the Block Diagram menu bar that slows things down and shows you data flowing through the wires)?  If you aren't going to study the answers that we provide to you and try to learn from them, what is the point of your asking more (irrelevant) questions?

 

Bob Schor

0 Kudos
Message 3 of 15
(5,929 Views)

Hi GerdW,

 

Thanks.

I've tried to implement notifiers. An error occured during execution (actally after click on "START" button).

It's the first time I try to use notifiers - I picked example from Core 2 and adapted it for my setup.

Where I've been mistaken.

 

Thanks

 

Two while loops in parallel - with notifiers.JPG

0 Kudos
Message 4 of 15
(5,911 Views)

Hi Pavel,

 

THINK DATAFLOW!

 

You kill your notifier before the upper loop finishes or even iterates the next time! The WaitOnNotifier will throw that error because of this deleted reference! Kill the notifer after both loops have exited!

 

Other solution: use the error out of the WaitOnNotifier to end the upper loop.

 

Both solutions are explained in example VIs coming with LabVIEW. And it seems you (again!) didn't debug your VI as suggested just before by Bob!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 15
(5,900 Views)

@Bob_Schor wrote:

Yesterday, you posed this same question under a different topic, and received several suggestions about how to accomplish what you seem to want.  In particular, I posted a single-loop simple method that did most of what you seem to want, and which you should be able to modify to suit your purposes.  Most of the solutions had single loops, none of them had local variables.

 

When you post a question on the Forum, do you actually try the code that is suggested?  Do you run it and see if it works?  If you don't understand how it works, do you turn on Execution Highlighting (the little light bulb on the Block Diagram menu bar that slows things down and shows you data flowing through the wires)?  If you aren't going to study the answers that we provide to you and try to learn from them, what is the point of your asking more (irrelevant) questions?

 

Bob Schor


... did most of what you seem to want

Most but not exactly the same ... your example starts immediately whereas mine case suppose some "setup" phase where parameters are adjusted

 

and which you should be able to modify to suit your purposes

it's exactly what I'm doing now - I picked from your example more correct loop management (while instead for) and try to adapt it to my case

 

When you post a question on the Forum, do you actually try the code that is suggested?

Always

 

Do you run it and see if it works?

Yes

 

If you don't understand how it works, do you turn on Execution Highlighting (the little light bulb on the Block Diagram menu bar that slows things down and shows you data flowing through the wires)?

Yes

 

If you aren't going to study the answers that we provide to you and try to learn from them, what is the point of your asking more (irrelevant) questions?

Always carefully study answers ... actually I try to explore several solutions and learn different techniques

 

Regards

0 Kudos
Message 6 of 15
(5,898 Views)

I can only suggest to repeat going through the learning materials (Core 1 and Core 2). It might be that you tried to "digest" or understand important and basic LabVIEW concepts too fast. You clearly miss of understanding how data flow works in LabVIEW. Try to repeat the teaching videos, and use the execution highlight so you can visualize how things work, and what kind of values you have at certain iterations on different wires.

0 Kudos
Message 7 of 15
(5,875 Views)

Hi GerdW,

 

Thanks for feedback. Well, I've looked through several examples that use notifiers and all differ form my case in one important detail:

in all of them while loops runs permanently, whereas in my case 1st loops stops immediately it sends notification.

I think for my case using of notifiers isn't the best solution ... flat sequence structure looks more adapted. Isn't it ?

 

Thanks

 

0 Kudos
Message 8 of 15
(5,871 Views)
Solution
Accepted by topic author Pavel_47

You want first a setup case, and after perform different number of steps, based on the setup parameters.

A perfect place for a state machine (only a single while loop required usually). You need an Idle state when you wait for the user inputs. After the user starts the initialize state, which will calculate the required number of steps for the other states. At the end the system automatically moves back to the Idle state, and waits for the user to setup and launch another run.

No need for Flat sequence structure at all. With a state machine you can just do much more (like aborting sequence in midway, not possible with sequence structures)

Message 9 of 15
(5,848 Views)

Why not simply add a Run button that determines whether or not you allow the computations to proceed inside the While loop?  Is this such a difficult concept to understand?

 

I think I must agree with the suggestions that you stop trying to learn LabVIEW by yourself (you're not doing a very good job of it, I fear) and spend some serious time taking as many of the on-line LabVIEW Tutorials, and working all of the examples you can find.  Write code, test your code, write more code, learn the basic principles, understand how Data Flow works, understand how Shift Registers work, understand how parallel loops work, and then come back to this (very simple) problem and tackle it "from scratch" (without looking at any your own or our posts).

 

Bob Schor

0 Kudos
Message 10 of 15
(5,842 Views)