LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Are the read and write operations atomic for an array in a local variable.


Hi,
 
I would like to know when you access an array in a local variable, is it an atomic operation?
 
Thanks,
 
Mat
0 Kudos
Message 1 of 10
(4,507 Views)

Hi Mat,

the write to a local must be an atomic operation. Else the array you mentioned might result buggy:   Consider writing to the local variable of an array at two locations on your block diagram without wiring any runtime dependeny to the write operations. If each write operation wires an array of a unique size (i.e. different from the size the other write operation does) and the write would not be atomic and thus interrupted by the scheduler - what would be the result if the write of the bigger array is interrupted in the middle of the write operation and now have the other write operation decrease the array size?   What should the scheduler do when it wants to resume the write of the larger array? Especially as the memory that was once allocated has been freed in between?  What to do if elements got lost that have already been written?   So, these must be atomic operations. (Let me say that I cannot prove this as I do not the source code of LabVIEW. So everybody who wants to add his comments is invited to do so.)

Furthermore, in the scenario mentioned above, you cannot say which write executes first in LabVIEW, therefore nobody should program this on a block diagram.

This makes your question quite confusing. If you cannot predict the result -> Why putting this on the block diagram?

When reading from local variables LabVIEW always creates an in-memory copy of the data (which you can easily verify by profiling the buffer allocations). This must be atomic, too. If not, the read could e.g. contain the nonsense from the not-atomic-write outlined above.

And now my question (because you succeeded in getting me curious):
What kind of block diagram were you thinking of when this question populated your mind?

Best regards, Guenter

0 Kudos
Message 2 of 10
(4,491 Views)

Hi,

   I agree with you but I would like to know if it is, fore sure, atomic (Can someone at NI confirm this??!!).

I am developping a Real-time application with Timed-loops. The Times-loops have different priority levels and loop rates. The faster loops have a higher priority (for a rate-monotonic sheduling).  I share data between the loops (which run on seperate threads). I don't want to use a RT-FIFO since the faster loop will read the value many times more than the slower one will write to it. That does not work with a RT-FIFO because with a FIFO once you read a value it is removed from the FIFO. I know I could use functionnal global variables (LV2 style global) but I don't whant to if I don't need to.

If writting and reading to an array are atomic operations then I can symply use local or global variables.

Thanks,

Mathieu  

0 Kudos
Message 3 of 10
(4,469 Views)
As Guenter mentioned, once you use a local or a global, a copy of the data is made and from that point on you have absolutely no way of knowing whether the data in the wire that comes from your local is synchronized with the data that is currently in the array indicator. If you're only writing to the array in one location and reading in multiple locations you should generally be OK, but if you want to be sure (and definitely if you want to write to the array from more than one place) you will have to implement some locking mechanism which will ensure the data hasn't changed since you read it (the synchronization VIs work for such mechanisms). If you wanted to avoid the LV2 globals to gain speed you would probably not like this much.

___________________
Try to take over the world!
0 Kudos
Message 4 of 10
(4,455 Views)

Thanks for the comments. I agree with you. However, I my case, race conditions and synchronization are not issues. Therefore, the only thing that matters to me is that the write and read operation of the array must be atomic. I know that I can implement that with a LV2 style global but I want to avoid it if possible.

 

If writing and reading to an array are atomic operations then I can simply use local or global variables.

 

All I need to know is: Is reading or writing an array in a local variable an atomic operation?

Thanks,

Mat


 

 

0 Kudos
Message 5 of 10
(4,450 Views)
Yes, "simultaneous" read and write operations on locals, globals are protected. They are atomic in the sense that one operarion is completed before the next one begins. You won't read a half updated cluster or array.


LabVIEW, C'est LabVIEW

Message 6 of 10
(4,446 Views)

Thanks Jean-Pierre. Did you find this out through experimention or is it stated somewhere in an NI document? 

Regards,

Mathieu

0 Kudos
Message 7 of 10
(4,437 Views)
Apparently my signature removes any credentials to my status of Proven Active Veteran...  Smiley Surprised

I got it most probably by a post from Greg McKaskle on info-LabVIEW or here for example.
Search the posts from Greg on locals to see this confirmed.

Now you can trust Greg, he is a LabVIEW developper.



LabVIEW, C'est LabVIEW

Message 8 of 10
(4,428 Views)


@JeanPierre wrote:
Apparently my signature removes any credentials to my status of Proven Active Veteran...  Smiley Surprised

Consider yourself lucky. At least you still are a PAV. Look at poor Kennon Cotton.

Molly?


___________________
Try to take over the world!
0 Kudos
Message 9 of 10
(4,414 Views)

I think the issue with Kennon Cotton's role is that his "minutes online" value has gotten so big that it's negative now.  (This is similar to what we saw in the past with Altenbach I believe.)  With a negative "minutes online" it's having problems calculating the correct role.  I have reported the issue.  Thanks for bringing it to my attention.

Have a great day!

Molly K.
Web Support & Operations Manager
National Instruments
0 Kudos
Message 10 of 10
(4,404 Views)