LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

sharing variables between concurrent while loops

I have a program that has 2 while loops running at the same time. For each, I have a dedicated Boolean variable that when TRUE, will exit the While Loop.

So I can click on one of the controls, that stops one loop, click on the 2nd control, the 2nd loop stops, & that causes the program to stop.

 

What I would like to do is to have the program stop whenever any one of the 2 controls is clicked.

I've tried ORing the control of one & the local variable control of the other into the While Loop Stop condition, but I've noticed that the local variable doesn't update the value of the variable that sits in the other loop... Is there a better way to make a variable's new value available in both loops simultaneously ?

 

 

ak 

0 Kudos
Message 1 of 9
(6,475 Views)

What do you mean by "simultaneously"?  Changing a control will all change the value that is in the local variable of that control.  But the local variable may or may not be read at the instant you want it to.  This is the essence of a race condition.

 

Why don't you post a simplified example of your code so we can see what you are trying to do?

0 Kudos
Message 2 of 9
(6,473 Views)
You could use a functional global to communicate between two loops.
Brian
0 Kudos
Message 3 of 9
(6,467 Views)

Hi Brian,

 

The functional global variable sounds interesting...but I have LabView 8.0. Would you be able to convert your example to 8.0 so I can see what you mean ?

 

  

  ak
 

0 Kudos
Message 4 of 9
(6,461 Views)
Here you go.  This should be 8.0.
Brian
Download All
0 Kudos
Message 5 of 9
(6,451 Views)

ak,

can you post your original vi.  It sounds like what you described works.  I coded up what I thought you were trying to say and it works fine.  It can also be accomplished with a single boolean rather than two.

loops.PNG

--
Tim Elsey
Certified LabVIEW Architect
0 Kudos
Message 6 of 9
(6,442 Views)

  Tim,

 

    Your program also works for me. But mine involves a Event Structure - see the attached picture of the Block Diagram.

 

ak 

 

 

0 Kudos
Message 7 of 9
(6,431 Views)

  Brian,

 

    Thanks - when I opened it up I got an error :

 

   Looks like I can get the program to stop by using the Application Control > Stop inside each of the 2 loops...

But it doesn't really teach me how to deal with variable value sharing when running multiple processes.

 

  ak
 

0 Kudos
Message 8 of 9
(6,430 Views)

The picture of your error message doesn't show because your picture file is located on your hard drive.  We can't access that from the web.  Don't use firefox's screenshot feature.  Attach your image file to your message.

 

 

Why didn't you actually attach your VI like I asked?

 

In general, multiple event structures can cause problems.  However, without seeing your actual VI, I can't tell if you'd have a problem or not.  If your code is structured just right, you can put an event case in each event structure that will handle the value change for both buttons.  So in loop 1, the event case is for value change of Stop1 and also value change for Stop2.  Same thing in Loop2

 

However, having two stop buttons where either one could stop the code is not very intuitive for the user.  There are many examples throughout the forum showing better ways to stop multiple loops, even ones that use local variables.  Also using queues to pass commands from a producer loop containing a single event structure to a consumer loop that would not have an event structure.

0 Kudos
Message 9 of 9
(6,415 Views)