04-26-2015 01:38 AM
Hi
How do we pass a particular data outside the 'For loop' after one iteration ? I would like to access the value of the tunnel after first iteration and not after the 'For loop' runs out completely.
Thanks
04-26-2015 02:10 AM - edited 04-26-2015 02:11 AM
Define what you mean by "access". Is the output tunnel set for "autoindexing" or last "value"?
It would be easier for us if you would show us your code, but it seems you simply need to start with a few beginner tutorials and learn about dataflow. Please explain in detail what you are trying to achieve, most likley there is a simple solution.
04-26-2015 03:09 AM
The fact that you want to pass a value out of the loop before the loop has finished suggests that you want to use that value elsewhere in your program while the loop is still running - is that correct? You can set the value of a local or global or functional global variable from within the loop and access it from elsewhere but you will have to be very careful to avoid race conditions. Better option is to use a notifier.
In the part of your code that uses the data, you need to wait until the value has been set. But how do you know when the value has been set? The notifier node waits until the value has been set, then gives you the new value.
Notifiers are in the Synchronisation>Notifier Operations and the help text gives plenty of information and examples.
If onthe other hand you just want a value from the first iteration to use after the loop has finished, use a conditional tunnel (right click tunnel and select 'Last Value' and 'Conditonal')
04-26-2015 12:52 PM
You can always use a Queue ...
Bob Schor
04-26-2015 07:37 PM - edited 04-26-2015 07:41 PM
@Stuart.Parkinson wrote:
The fact that you want to pass a value out of the loop before the loop has finished suggests that you want to use that value elsewhere in your program while the loop is still running - is that correct? You can set the value of a local or global or functional global variable from within the loop and access it from elsewhere but you will have to be very careful to avoid race conditions. Better option is to use a notifier.
You should never give this advice. Using globals/locals here is beyond terrible. It's not so much a race condition you'll be worrying about in this case. Race conditions suggest multiple writers racing to write the value. The issue is the variable isn't always in sync with the reader. It may read the same value multiple times or miss values entirely. When the poster clearly doesn't understand data flow, the single worst thing you can do is give advice like this. Please be more careful.
Beyond that, you should take a look at the difference between a queue and a notifier. You're using them incorrectly if you're using them how you suggest here. The purpose of a notifier is to send a single value to multiple places. This can also be lossy if the writer is sending notifications faster than they are being read. That's not what the poster would want. A queue creates a buffer that saves all the values and is read by a single reader. There are three solutions the poster should consider:
1) output their data as an array and process it after the loop
2) use a queue to send the value to a parallel loop for processing
3) move the processing logic into the for loop
04-27-2015 12:57 AM
You should never give this advice.
Advice on how not to do it...
The phrases "have to be very careful" and "But how do you know when the value has been set?" were intended as a warning, not advice, and I am sorry if that intent did not communicate well.
A notifier. Written in one place, read by many. A queue. Writable in many places, read by one. The application determines the correct one to use (and I don't know the application). My answer was incomplete for omitting the queue, not incorrect for suggesting a notifier. From the LV documentation, "Use the Notifier Operations functions to suspend the execution of a block diagram until you receive data from another section of the block diagram..." In response to the question, "But how do you know when the value has been set?" it is a perfectly legitimate answer.
I understand if you would like to add further information that you feel would be useful to the discussion, but no need to stamp so heartily on others that are trying to do the same.
04-27-2015 01:39 AM
@natasftw wrote:
... This can also be lossy if the writer is sending notifications faster than they are being read. That's not what the poster would want. ...
Actually, the poster wants to send a single value, once, after the first iteration of the loop completes. I don't think 'sending notifications faster than they are being read' comes into it.
04-27-2015 01:45 AM
If your use case is that you want to update an indicator inside the loop, simply access it inside the loop:
Thanks,
Danielle
04-27-2015 07:13 AM
There is obviously a lot of confusion about what is being asked. So before we go too far here, we need to wait for adarshn to come back an clarify their requirements.