370 likes | 395 Views
Lesson 2 Communicating Between Multiple Loops. Variables Functional Global Variables Race Conditions Synchronizing Data. A. Variables. Cannot pass data between parallel loops with a wire
E N D
Lesson 2Communicating Between Multiple Loops • Variables • Functional Global Variables • Race Conditions • Synchronizing Data
A. Variables • Cannot pass data between parallel loops with a wire • Variables allow you to circumvent normal dataflow by passing data from one place to another without connecting the two places with a wire
A. Variables • Variables are block diagram elements that allow you to access or store data in another location • Variables can be of the following types: • Local: store data in front panel controls and indicators • Global: store data in special repositories that can be accessed from multiple VIs • Functional Global: store data in While Loop shift registers • Shared: transfers data between various distributed targets connected together over a network
A. Variables – Using in a Single VI • Use local variables to pass data within a single VI
Creating Local Variables • Create and use local variables.
A. Variables – Using Between VIs • Use a global variable or a single process shared variable to share data between multiple VIs • Use a global variable to share data among VIs on the same computer, especially if you do not use a project file • Use a single process shared variable if you may need to share the variable information among VIs on multiple computers in the future
Creating Global Variables • Create and use global variables.
Creating Shared Variables • Create and use single process shared variables.
A. Variables – Initializing • Verify that variables contain known data values before the VI runs • If you do not initialize the variable before the VI reads it for the first time, it contains the default value of the associated front panel object
B. Functional Global Variables • The general form of a functional global variable includes an uninitialized shift register with a single iteration For or While Loop
B. Functional Global Variables • A functional global variable usually has an action input parameter that specifies which task the VI performs • The VI uses an uninitialized shift register in a While Loop to hold the result of the operation
B. Functional Global Variables – Timing • Very useful for performing customized elapsed time measurements
Exercise 2-1: Variables VI • Use variables to write to and read from a control.
C. Race Conditions • A race condition is a situation where the timing of events or the scheduling of tasks may unintentionally affect an output or data value • Race conditions are a common problem for programs that execute multiple tasks in parallel and share data between the tasks
Race Conditions • Watch the instructor demonstrate race conditions. • C:/Exercises/LabVIEW Basics II/Demonstrations
C. Race Conditions • Race conditions are very difficult to identify and debug • Often, code with a race condition can return the same result thousands of times in testing, but still be capable of returning a different result • Avoid race conditions by: • Controlling shared resources • Properly sequencing instructions • Identifying and protecting critical sections within your code • Reducing use of variables
C. Race Conditions – Critical Code • A critical section of code is code that may behave inconsistently if some shared resource is altered while it is running • If one loop interrupts another loop while it is executing critical code, then a race condition can occur • Eliminate race conditions by identifying and protecting critical code with: • Functional Global Variables • Semaphores
C. Race Conditions – Critical Code • Functional Global Variable used to protect critical code:
C. Race Conditions – Sequencing • What is the final value? • Four possible outcomes: • Value = (Value * 5) +2 • Value = (Value + 2) * 5 • Value = Value * 5 • Value = Value +2
Exercise 2-2: Bank VI • Eliminate a race condition by protecting a critical section of code.
D. Synchronizing the Transfer of Data • Variables are one method for passing data between parallel processes • Using variables breaks the LabVIEW dataflow paradigm, allows for race conditions, and incurs more overhead than passing the data by wire
Notifiers • Open the Master/Slave Design Pattern template in LabVIEW and explore the operation of Notifiers.
D. Synchronizing the Transfer of Data • The following benefits result from using Notifiers to transfer data between parallel loops: • Both loops are synchronized to the master loop—the slave loop only executes when the master loop sends a notification • You can use Notifiers to create globally available data, making it possible to send data with a notification • Using Notifiers creates efficient code—there is no need to poll to determine when data is available from the master loop
D. Synchronizing the Transfer of Data Notifier disadvantages: • A notifier does not buffer data • If the master loop sends another piece of data before the first piece of data has been read by the slave loops, that data is overwritten and lost
D. Synchronizing the Transfer of Data • Queues are similar to notifiers, except that a queue can store multiple pieces of data • By default, queues work in a FIFO (first in, first out) manner • Use a queue when you want to process all data placed in the queue • Use a notifier if you only want to process the current data
Queues • Open the Producer/Consumer Design Pattern (data) template in LabVIEW and explore the operation of Queues.
Weather Station • Demonstrate the use of queues for data transfer and synchronization of parallel loops. • C:/Exercises/LabVIEW Basics II/Demonstrations/Case Study - Queue
Exercise 2-4: Global Data Project • OPTIONAL • Create a project containing multiple VIs that share data using a single process shared variable.
You should use variables in your VI where ever possible. True False Which cannot transfer data? Semaphores Functional global variables Notifiers Queues Which can you use only within a project? Local variable Global variable Functional global variable Single process shared variable Which cannot be used to pass data between multiple VIs? Local variable Global variable Functional global variable Single process shared variable Summary—Quiz