Multi-task data access synchronization in the PLC

When one set of data is accessed by multiple tasks, the tasks may access the same data simultaneously, depending on the task/real-time configuration. If the data is written by at least one of the tasks, the data may have an inconsistent state during or after a change. To prevent this, all concurrent accesses must be synchronized so that only one task can access the shared data at a time.

These concurrent accesses from several tasks that require synchronization include the following cases, for example:

In brief: If one set of data is accessed by several tasks and the data is written by at least one of these accesses, all read and write accesses must be synchronized. This applies regardless of whether the tasks are run on a single or multiple CPU cores.

WARNING

Risk of injury due to unforeseen axis movement

If concurrent accesses are not synchronized, there is a risk that the data records will be inconsistent or invalid. Depending on how the data is used in the further course of the program, this can result in incorrect program behavior, undesired axis movement, or even a sudden program standstill. Damage to equipment and workpieces may occur, or people's health and lives may be endangered, depending on the controlled system.

  • Synchronize concurrent accesses.
  • You will find function tests with corresponding explanations in the sample programs for the MUTEX procedures, which demonstrate why it is necessary to synchronize accesses.

Synchronization options

The following options are available for synchronizing accesses:

1) Mutex procedure (TestAndSet, FB_IecCriticalSection) for securing critical sections

  • The number of critical sections must always be kept as small as possible.
  • The critical sections must be kept short.
  • Use FB_IecCriticalSection for comparatively short critical sections.
  • Use TestAndSet for comparatively long critical sections.

2) Data exchange via the PLC process image

  • This variant is only possible and recommended if only one task has write access to the same data.
  • The possible data volume is limited due to necessary internal copy actions.

3) Data exchange via synchronized buffers

  • This variant is only possible if only one task has write access to the same data.
  • This is a user-specific implementation for which there are various options.
  • Access to the individual buffers must be secured, for example with TestAndSet().
  • The possible data volume is limited due to executed copy actions.

Synchronization also in the case of atomic access

The necessity for synchronization normally also applies even if a single access to a variable (e.g. writing an integer) could be described as atomic, i.e. uninterruptible.

Because the property of the atomic access depends among other things on the processor architecture used, every access should be regarded as non-atomic for simplicity's sake and in particular for safety.

Note that even supposedly safe accesses almost always turn out to be unsafe when considered more closely. This is explained below with the help of two example scenarios:

Further information