Local, Global, or Network Shared Variable Can Cause Race Conditions

Updated Jul 9, 2018

Reported In

Software

  • LabVIEW Full
  • LabVIEW NXG
  • LabVIEW Full for Academic

Issue Details

  • I have two independent while loops running in parallel. The first while loop is acquiring data and storing it in a local, global, or shared variable. The second loop is periodically reading the data from a local, global, or shared variable. Most of the time everything runs smoothly, but sometimes I lose data and the variable becomes empty. How do I prevent this?
  • I have a Network Shared Variable that is used on the host computer while other┬ácomputers are reading the variable.

Solution

Using two parallel loops to write to and read from the same variable, or trying to access a Network Shared Varible while a value is being written to it, can cause a race condition.

  • The first case occurs when one loop is reading the value of a local, global, or shared variable, and one is writing to the value of the local, global, or shared variable. If they are not synchronized, then the value could be read before it has actually been sent or vice-versa.
  • The second case happens when one computer is writing data to the Network Shared Variable and other computers try to access the value at the same time.


Queues implemented in a Producer/Consumer architecture are most commonly used to avoid data starvation or data overflow. Using a Producer/Consumer architecture provides a synchronization advantage because the Consumer loop only executes when there is data available in the queue.

Another alternative is to use Semaphores to lock the variable resource while you write to it.

WAS THIS ARTICLE HELPFUL?

Not Helpful