From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to pass a value from one loop to another

Forgot the attachment:

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 11 of 18
(1,392 Views)

Hi Tbob,

 

You wrote "scaring newcomers away from locals and globals like they are the plague".

 

Global variables are the plague in my book. I lived through that affliction once and I do not plan on doing it again.

 

There are very specific cases were it is possible to "get away" with using locals and globals. These cases all fall into the catagory of "single writer, multiple reads".

 

Local variables are usefull in updating the user interface but agian I have to be careful that I only have one writter at a time. As far as using them to pass info from loop to loop, your example shows the case were they can be used. If speed is not an issue and the data being passed is small, we not see much difference. When the data set get large the copies required for each instance of a local start gobbling up memory.

 

The next concern that keeps me away from globals is expansion and flexibity. The "local and global" route sets me up for disaster when the customers tells me "Could we have an erro in the DAQ read force the application to shutdown?".  This is where the race condition comes in because it would be whichever writes last will win the update game (race condition).

By using LV2 globals or queues my code can be adapted without having to go back and eliminate all of the race conditions. LV2 globals will also quickly adapt into "action engines" that can handle more complicated situations like "Clear Run flag if set" which just can not be coded in a global or a local.

The attached image illustrates how I use a queue to pass commands from the UI loop (handles repsonding to user clicks) and the Execution loop that does the long drawn on tasks.

In the top loop I use a local only to signal the user that the button click was recognized. The bottom loop previews the queue to determine if a command was sent and only if the queue is not empty, will the queue be read.

So....

I discourage the use of globals in "ALL" cases. Locals are OK for updating user interface.

Trying to help,
 
Ben

Message Edited by Ben on 07-19-2005 12:24 PM

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 12 of 18
(1,383 Views)

1) what is an LV2 Global and where can I start learning the basics

2) i am assuming the shipping example of queue basics is where i should start learning about those?

Ryan

LV 7.1
0 Kudos
Message 13 of 18
(1,361 Views)
The reason I tend to use a queue for sending data between loops is to enforce some degree of determinism in the concurrent system.  I agree with you that locals can work in some cases but other cases like multiple writes or looking for events with polling locals can fail.  You can prove determinism in a multithreaded application with a shared variable in the single read, single write case.  I could be wrong though, I last took a formal course course in concurrency a few years ago so it is all not that fresh in my mind.  I admit to using locals to solve interthread communications too, I guess the answer to this is very application specific.  Are there any situations where a queue fails for deterministic communication and are there situations where queue overhead becomes overwellming.  I know in c/c++ this is a much harder problem and am thankful labview saves us from much of the stress of multithreaded programming.
 
Paul Falkenstein
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 14 of 18
(1,359 Views)
I think I am being misunderstood.  I am not advocating using locals or globals in all instances where they can be used.  That is absurd.  What I am saying is that it is far better to learn how to use them properly than to avoid them completely.  Learn how to recognize potential race conditions and then don't use them where this may be a problem.  If a race condition will not occur, why not use them if there is an advantage to using them?  I have had numerous problems in the past because of race conditions.  What I did was to learn how to recongnize them rather than avoid using locals and globals.  Because I have trained myself to look for race conditions, or even possibilities of race conditions, I have a greater number of tools to use (locals and globals) than those who choose not to mess with looking for potential problems.  It has been a very long time since I had to deal with a race condition because of the knowledge I gained from this experience.  With that said, I will normally avoid using them unless I see that there is a big advantage to using them and that it will not cause problems.  I really don't use them if I don't have to.
Its all about personal preferences.  It is also very application specific.  I prefer to keep things as simple as I can.  So I will use a local instead of a queue or a LV2 functional global if I can see that it will not cause a problem. Others may prefer to use other methods so that they don't have to worry about potential problems.  Both are OK.  But we still should not be scaring newcomers away from locals and globals, we should be educating them and letting them make their own choice.  I am working on a complex program right now (close to 1000 VIs) and I use globals to hold GPIB addresses, IP addresses, and other constants.  They work great and there is no chance of any race conditions.  So why should I avoid them because somebody else thinks they are a plague.  I would tell newcomers that they are only a plague if you don't intend to learn how to use them properly.  Make the extra effort to learn about race conditions and your programming efforts will become easier.
For the person wanting to know about LV2 functional globals, here is an attachment that shows one.  Inside the block diagram is an explanation of how it works.
 
- tbob

Inventor of the WORM Global
0 Kudos
Message 15 of 18
(1,348 Views)

guess i should have mentioned i have 7.0

sorry 😕

Ryan

LV 7.1
0 Kudos
Message 16 of 18
(1,344 Views)
No problem:
- tbob

Inventor of the WORM Global
0 Kudos
Message 17 of 18
(1,338 Views)

Thank you very much for all your responses. I learned lot and managed to solve my problem by using local variables. If I face anyproblems, I may consider queues.

Regards,

Ravi.

0 Kudos
Message 18 of 18
(1,299 Views)