Skip to main content
Renesas Electronics America - Knowledgebase

Using Mutexes

Latest Updated:10/13/2016

What is a Mutex?

A mutex is basically a binary semaphore, which means that only one thread can own a mutex at a time. In addition, the same thread may perform a successful mutex get operation on an owned mutex multiple times, 4,294,967,295 to be exact. There are two operations on the mutex object: tx_mutex_get and tx_mutex_put. The get operation obtains a mutex not owned by another thread, while the put operation releases a previously obtained mutex. For a thread to release a mutex, the number of put operations must equal the number of prior get operations. Each mutex is a public resource. ThreadX places no constraints on how mutexes are used.

When should a Mutex be used?

ThreadX mutexes are used solely for mutual exclusion. Unlike counting semaphores, mutexes have no use as a method for event notification. The ideal use of a Mutex is to control execution of critical sections.

What happens if a thread tries to acquire a mutex owned by another thread?

Application threads can suspend while attempting to perform a get operation on a mutex already owned by another thread. After the same number of put operations are performed by the owning thread, the suspended thread’s get operation is performed, giving it ownership of the mutex, and the thread is resumed. If multiple threads are suspended on the same mutex, they are resumed in the same order they were suspended (FIFO). However, priority resumption is done automatically if the mutex priority inheritance was selected during creation. Priority resumption is also possible if the application calls tx_mutex_prioritize prior to the mutex put call that lifts thread suspension. The mutex prioritize service places the highest priority thread at the front of the suspension list, while leaving all other suspended threads in the same FIFO order

What is a deadly embrace?

One of the most interesting and dangerous pitfalls associated with mutex ownership is the deadly embrace. A deadly embrace, or deadlock, is a condition where two or more threads are suspended indefinitely while attempting to get a mutex already owned by the other threads.

What is priority inversion?

A major pitfall associated with mutual exclusion is priority inversion. The basic problem results from a situation in which a lower priority thread has a mutex that a higher priority thread needs. This in itself is normal. However, threads with priorities in between them may cause the priority inversion to last a nondeterministic amount of time. Unlike semaphores the ThreadX mutex object has optional priority inheritance.

What is priority inheritance?

The basic idea behind priority inheritance is that a lower priority thread has its priority raised temporarily to the priority of a high priority thread that wants the same mutex owned by the lower priority thread. When the lower priority thread releases the mutex, its original priority is then restored and the higher priority thread is given ownership of the mutex. This feature eliminates undeterministic priority inversion by bounding the amount of inversion to the time the lower priority thread holds the mutex.

Where can I find more information?

You can find more information on the ThreadX user manual, available here



©1997-2015 by Express Logic, Inc. All rights reserved. This document and the associated ThreadX software are the sole property of Express Logic, Inc. Each contains proprietary information of Express Logic, Inc.

  • Was this article helpful?