LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

usage of shared variable

I'm trying to find out something about shared variables.  If I have a writer sending data to a shared variable, and a reader acquiring the data from the same shared variable, will the shared variable prevent competition betweent he writer and the reader, i.e. the data will be kept in sequence all the time?  And also, what structure should I use to make a good reader which always updates itself from the shared variable?  Thanks in advance.
0 Kudos
Message 1 of 8
(4,060 Views)

What do you mean by "kept in sequence"?

 

The use of shared variables can have the same problems of race conditions as local variables and global variables.  If you are just using a single element shared variable, you can read the same data point twice, or miss a data point if you have 3 writes but only 2 reads.

 

But if you use network buffered shared variables, then they can act as a queue, and store the values of all the writes to the shared variable in order.

 

Take a look at these for more details.

 

Using the LabVIEW Shared Variable

 

How Does Buffering Work for Shared Variables?

Message 2 of 8
(4,055 Views)

I do just use a single element shared variable, and I only have one writer and one reader but they write and read at the same time.  And the race conditions is what I'm worried about.  So should I use network buffered shared variable?  I know functional global can do it.

0 Kudos
Message 3 of 8
(4,050 Views)

What kind of data are you passing through these shared variables?  Is it something simple like a status boolean, or a numeric value where you only care about its current value, or if its value is one iteration old, it doesn't matter?  Or is it a situation where you want to see all of the values that are ever passed through it so the loss of one single datapoint is an issue, or the duplication of a single data point is an issue?

0 Kudos
Message 4 of 8
(4,033 Views)
It's a series of numbers: 0, 1, 2, 3,... 9, one at a time.  But as you said, I want to see all of them passing through one by one, NO loss or duplication.
0 Kudos
Message 5 of 8
(4,031 Views)
These are the VI's that I wrote to solve a problem, which is to read from the front panel a value that is being changed by a subVI.  I want to read every single value not just the final one.  I tried functional global and now I want to try shared variable.  And I was worried about race condition.  Thank you.
0 Kudos
Message 6 of 8
(4,025 Views)

Hi graffaner,

 

As Ravens Fan pointed out, if you use a single element shared variable, you run the risk of running into race conditions. More specifically, the risk of losing data points. Look into using network buffered enabled shared variables to avoid this. As far as duplication is concerned, you can enable the timestamp of the shared variable when reading from it, which will display the time that it was last updated. You can then compare this to the last timestamp, probably using shift registers in some way, from the last time you read from the shared variable to see if that value has been updated.

 

Hope this helps!

 

Thank you for choosing National Instruments.

 

Aaron P

National Instruments

Applications Engineer

http://www.ni.com/support

0 Kudos
Message 7 of 8
(3,990 Views)
Thank all of you very much.  I think I'm gonna try functional global again.  Seems like it will do what Aaron was saying I need to do.
0 Kudos
Message 8 of 8
(3,987 Views)