LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to access value from inside while loop outside of the loop

I have 2 parallel while loops.

There is a variable(runs from 0-50) in one that I would like to access simultaneously in the other loop...I tried createing a global variable but it seems that it is only reading the very first value(0). I know there might possibly be a race condition, but shouldnt the variable be updated in the second loop continously anyways?

 

I  have read this thread already but still couldn't solve the problem:

http://forums.ni.com/t5/LabVIEW/How-to-send-value-from-inside-while-loop-to-the-outside/m-p/2511346/...

 

Thanks!

0 Kudos
Message 1 of 12
(5,407 Views)

Use Queue/Notifier Functions to Comunicate/share data among the loops.Variables are slow and unpredictable :[ 

 

Can u show some code?

0 Kudos
Message 2 of 12
(5,391 Views)

so you mean global variables won't work?

Actually my code is too big to post, since this a minor part of a large program,

But basically I have the producer/consumer pattern, where I enqueue the elements in the producer and dequeue them in the consumer.

 

I dequeue 8 times, and build an array of 1x8. Then I want to insert this array into a bigger 2D array, using replace subset. The counter (which is my global variable) is created in the producer loop (0-50).

The new 2D array will be 50x8, so the index for the replace array subset would be the counter (0-50).

I want to store each 1D array into a separate row of the 2D array, since I will need all the values later.

 

Hope this is clear...If not I can make a sample code of what I mean.

0 Kudos
Message 3 of 12
(5,388 Views)

You should be careful when you maintain state information across loops. In addition, global variables are not the best method for conveying information. They can easily lead to race conditions. They also highly couple your code making it more difficult to ever reuse portions of it. Generally state information, such as your array index, should be maintained in one process. Other tasks that are posting to a queue should simply post the data. When tasks are tightly coupled debugging can become much harder. If you are getting some count in one porcess and that will state where the data should be placed than instead of having a global or local variable change your message content to a cluster that includes both the data and the index. I generally prefer a messaging format that is a cluster of a message type, either a string or an ENUM, and a variant. This messaging construct can pass basically any type of data. Only the message producer and message consumer need to worry about the message content itself. This consttruct is very flexible and easy to extend.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 12
(5,371 Views)

Producer Loop means "Generate" usable Data.

Consumer Loop means "do something useful" with that Data.   Sounds Poetic....

 

Therefore.... (If i understand correctly ur situation...)

 

Why u not dequeue the whole 1D Array instead of building that element by element in consumer loop? 

Then u can use replace subset directly to 2D Array... No need for that "Counter".

OR

U can create an Large 1D Array with all elements, and after finish the "dequeue process" ,u can reshape it to 2D Array (U know the final size)

OR

U can put that counter in Consumer Loop with Shift Register and Increment that at 8th iteration (What means One Array already dequeued).

 

Choose one.

 

0 Kudos
Message 5 of 12
(5,349 Views)

why dont you pass a reference?

0 Kudos
Message 6 of 12
(5,332 Views)

@apok wrote:

why dont you pass a reference?


That isn't much better than using a global/local. Actually, it can be worse since it will force the task to run in the UI thread. I try to avoid using refernces solely for the purpose of passing data around.I try to use them only for VIs that are updating the UI itself and I will have separate tasks specifically for handling the UI. Processing code will be separate tasks which are UI agnostic.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 7 of 12
(5,307 Views)

@developer001 wrote:

I dequeue 8 times, and build an array of 1x8. Then I want to insert this array into a bigger 2D array, using replace subset. The counter (which is my global variable) is created in the producer loop (0-50).

The new 2D array will be 50x8, so the index for the replace array subset would be the counter (0-50).

I want to store each 1D array into a separate row of the 2D array, since I will need all the values later.

 

Hope this is clear...If not I can make a sample code of what I mean.


Replace array dont increase array size (build or insert does), so unless you've initialized an array alread you'll stay at 0 items, which'd give the behaviour you describe.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 12
(5,302 Views)

Yes I have already initialized the array to be 50x8.

0 Kudos
Message 9 of 12
(5,272 Views)

@developer001 wrote:

Yes I have already initialized the array to be 50x8.


Post the VI.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 10 of 12
(5,269 Views)