Multithreading Synchronization

Easiest way to serialize access to a procedure from multiple threads is to use the procedure Synchronize property, the property ensure that only one thread execute a procedure while the other threads calling the procedure will wait until the first thread procedure returned. The procedure can be used to serialize access to global variables or to other shared resources such as instruments. See Procedure Properties for more information.

Synchronization classes can also be used when access to a resource must be controlled to ensure integrity of the resource. Synchronization access classes are used to gain access to these controlled resources. Here is when to use each class.

To determine which synchronization class you should use, ask the following series of questions:

Does the application have to wait for something to happen before it can access the resource (for example, data must be received from a communications port before it can be written to a file)?

Can more than one thread within the same application access this resource at one time (for example, your application allows up to five windows with views on the same document)?

Can more than one application use this resource (for example, if the resource is in a DLL)?

Example: Using Three Synchronization Classes

As an example, take an application that maintains a linked list of accounts. This application allows up to three accounts to be examined in separate windows, but only one can be updated at any particular time. When an account is updated, the updated data is sent over the network to a data archive.

This example application uses all three types of synchronization classes. Because it allows up to three accounts to be examined at one time, it uses ASemaphore to limit access to three view objects. When an attempt to view a fourth account occurs, the application either waits until one of the first three windows closes or it fails. When an account is updated, the application uses ACriticalSection to ensure that only one account is updated at a time. After the update succeeds, it signals AEvent, which releases a thread waiting for the event to be signaled. This thread sends the new data to the data archive.