-

MUTEX

Type Function block
Description The object-oriented MUTEX function block is used to manage the access to shared resources across multiple tasks. It is used to make access to these shared resources mutually exclusive. The FB prevents race conditions, data corruption, or unexpected behavior when multiple tasks try to access shared resources at the same time.

How a Mutex works

The function block provides the following methods that are used for interacting with a mutex:

  • LOCK

    Locks the mutex (becomes the owner of the mutex). If the mutex is already locked by another task, the task will wait (block) until it's available (unlocked by another task).

    Note
    In case of a mutex lock, a task watchdog can occur. It is therefore recommended to use a TRYLOCK method instead of a LOCK method.

  • UNLOCK

    Unlocks the mutex, allowing the next waiting task to become the owner of the mutex (to lock the mutex).

    Must always be called after a successful TRYLOCK or LOCK (return value TRUE).

    Note
    Only the task that locked the mutex is able to unlock it.

  • TRYLOCK

    Tries to lock the mutex (to become the owner of the mutex). It checks if the mutex is available:

    • If yes - it locks the mutex and returns TRUE.
    • If no - it returns FALSE immediately, without waiting.
  • IS_ENTERED

    Checks whether the current task holds the lock on the specified mutex.

    • TRUE = Task has locked the mutex
    • FALSE = Task has not locked the mutex.

Further Info
See the section "Protecting concurrent access to shared data with Mutex and Semaphore" in the topic "Global Function Blocks in PLCnext Engineer" for further details on mutexes.

Further Info
See the topic "Methods of FBs in PLCnext Engineer" how to use function block methods.