LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you synchronize accesses to a LabVIEW Shared Variable?

I would like to create an ad-hoc weather station program (I'll explain more in a bit).  I am using LabVIEW 8.0 Full Edition, and I would like to share data over a network between stations with the LabVIEW Shared Variable.  Here's what I want to be able to do:

  1. A node would start up, and begin publishing data to a network via a shared variable.
    • The shared variable is an array of clusters
    • The cluster information would hold things like:
      • Station Name
      • Station Location
      • Weather information cluster (temperature, rainfall, windspeed, wind direction, etc...)
      • Timestamp of last update
  2. When a new node would like to enter, it would bind to the shared variable, grow the array, and add its information.
    • If a node's Station Name and Station Location is already in the shared variable, it would merely update the information in the cluster.
  3. Viewing nodes could pop in, bind to the shared variable, and read/display the information at any time. 

I am trying to enumerate problems with this before implementing, and I have run into a stinker of a problem that I am not sure how to solve.  How do I synchronize accesses to the LabVIEW Shared Variable?  If I read the variable, modify it, and write it back, I will undoubtedly run into a race condition where 2 nodes attempt to update its data and I will lose the data written by the first node - Node A reads, Node A modifies, Node B reads, Node A writes, Node B Modifies, Node B Writes, and thus the modifications made by Node A are lost.  In my specific application losing some data isn't critical, but if not remedied this type of problem could cause massive amounts of data to be lost when there are numerous nodes, and that is definitely not acceptable. 

Does anyone have any recommendations on how to synchronize the read-modify-write operations on the data in the Shared Variable?

-Danny

Message 1 of 4
(4,181 Views)
Hello texasdiaz,

As you would with a local or global variable, you can choose to use semaphores to lock the variable resource as you write to it.  You might want to take a look at some of the semaphore shipping examples (Semaphore with SubVI, for example) for more details on how to create and manage a semaphore in LabVIEW.

Hope that helps.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 4
(4,164 Views)
Wendy,
 
I am afraid Semaphores are not network-shared objects (to my knowledge), they are system-level objects that use operating-system synchronization mechanisms.  If I were synchronizing on a single machine, a semaphore might be a valid mechanism; as an aside, most user-mode semaphores are designed to synchronize within a single process - to synchronize between processes you need to store the semaphore in the Kernel, and to synchronize over a network you would need a network node to handle serialization of requests.  My Shared Variable is published over a network, and to my knowledge there are no network-published synchronization mechanisms available - mostly because there is no way to currently perform an atomic test-and-set on the Shared Variable (am I correct?) and there isn't a mechanism for blocking access to a Shared Variable from another network device/machine (or is there?).  I've been looking for some way to implement an atomic test-and-set but I am running into a wall; I know that if I select the "single writer" attribute of the Shared Variable I can get LabVIEW to force a single writer allowing me to have an atomic "set", but I need more than that.
 
If only there was a network-shared Semaphore or some way to block other network accesses to the Shared Variable I would be in business - something like what I want doesn't exist, does it?
 
Thanks!
-Danny
 

Message Edited by texasdiaz on 02-23-2006 02:52 AM

Message 3 of 4
(4,159 Views)

Yes Danny, that is a big short-coming in the Shared Variable and it inherited that issue from the traditional global.

If you have not seen it yet, you may want to review post #2 in this thread

http://forums.lavausergroup.org/index.php?showtopic=2548

on the LAVA forum for general info on the shared variable.

There should be ways of implementing what you are after but that solution would be in the architecture and not in the global.

If anyone has this figured out already, I love to hear your thoughts!

Ben

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