LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreading, question about data protection

I have a global variable that can be accessed from two threads, but only one
of them can write to it [the second only reads the variable].
The question is: Do I have to protect the global variable from simultaneous
access ?


____________
regards
Frog
0 Kudos
Message 1 of 3
(2,958 Views)
If you have 1 and only 1 thread writing to the variable, then whether you have to protect it depends on the type of the variable. If the type is a type that the OS guarantees gets written in an atomic manner, you do not actually have to protect it. This, however, introduces some brittleness to your program that is probably not worth the time you save by not protecting the variable. If you update your program in the future to use more than 1 writing thread and/or you change the variable so that it no longer gets written atomically your program will break in some obscure and unreproducible manner.

If you do decide to protect the variable, the simplest thing to do is to use the CVI Utility library Thread Safe Variable macros. The documentation for these exists in th
e function panel class help for the Thread Safe Variable functions (Library>Utility>Multithreading>Thread Safe Variable).

For more information, see the Multithreading in LabWindows/CVI whitepaper (Start>Programs>National Instruments>Measurement Studio>Help>Measurement Studio Library>Concepts and Techniques LabWindows/CVI>Multithreading in LabWindows/CVI).

David Rohacek
National Instruments
0 Kudos
Message 2 of 3
(2,958 Views)
Big thanks.

BTW.
Is there a way to pass array of structures to mentioned macros ?

_______
regrads
Frog
0 Kudos
Message 3 of 3
(2,958 Views)