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 write in Clusters and reading it avoiding race conditions

Hi everyone,

 

I was wondering if it is posible to write to a cluster and reading it elsewhere without having race conditions.
If the cluster reference is inside the loop that writes data into it, I am pretty shure there will danger for race conditions, as there is two "read cluster" elements (Pic 1).
Would it be sufficient, if I place the cluster reference outside the writing loop? (Pic. 2)

And is it posible to write two copies of a cluster with just one bundle by name element? Or do I need to add one bundle by name per Cluster? (Pic 3)

 

I am using clusters, because I want to keep the number of Network Published Variables to a minimum. If the answer to question of pic 2 is no, do you suggest making an array per data type instead?

 

All Screenshots are examples, and not actual code.

 

Greets, Mathias

0 Kudos
Message 1 of 3
(962 Views)

If you want to make sure that there is only a single access to the cluster data at any one point in time you should either implement acess to it through a functional global ar wrap it in a data value reference. Either will ensure that any read/update is handled by only one entinty acting on the data itself. I would really question why you are using so many local/global variables in your code. You would be much better off doing this via wires or messaging.

 

Now, with that said, anytime you have multiple actors on a single piece of data you can have race conditions. Even if you protect access using one of the above suggestions. If you need multiple parts of your system that is accessing the same data to be sychronized, you would have to use sort of messaging system and state information to determine if it is appropriate for the various parts to access the data. For example, you have multiple code loops accessing some data but you have one task that initializes the data and others that will access it when executing and they need to wait until it has been initially, you will need some type of state variable or message to let those other tasks know that initialization has been completed and the data is valid.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 3
(943 Views)

Carefully listen to Mark.

 

Still, you only have a single writer and it is not clear what you consider a "race condition". If the readers should wait until the values have been updated, other mechanism are better. To wire a cluster template, one would typically use a typedef'd diagram constant, not the actual shared variable. Does the bundle by name replace all elements or just some?

 

Typically you can expect significantly better help by attaching a simple VI. Truncated images are not useful, especially if you are not even embedded into the posts. Too much clicking for us!

0 Kudos
Message 3 of 3
(887 Views)