
ThreadX gives you two distinct tools for controlling preemption and bounding priority inversion: preemption threshold and priority inheritance. They look similar on paper — both raise effective priorities — but they operate at different layers, trigger under different conditions, and solve different problems. Confusing them leads to missed deadlines, unexpected preemption, or worse: a system that works in testing but fails under load.
This article walks through the mechanics of each, shows how to diagnose which one you actually need, and demonstrates the TraceX workflow to prove your fix works.
+------------------------------------------------------------------+| PREEMPTION THRESHOLD || Thread sets a ceiling: "Nothing below priority 15 preempts me" || + Higher-priority threads (0-14) STILL preempt || + Medium-priority threads (15-20) BLOCKED from preempting || + Configured per-thread, changed at runtime || - Disables time-slicing for this thread |+------------------------------------------------------------------++------------------------------------------------------------------+| PRIORITY INHERITANCE || Mutex owner inherits waiter's priority WHEN waiter blocks || + Only activates on mutex contention || + Bounds inversion to mutex hold time || + Requires TX_MUTEX_ENABLE_PRIORITY_INHERITANCE at build || - Does not prevent preemption by unrelated medium threads |+------------------------------------------------------------------+
ThreadX preemption threshold is a per-thread ceiling. A thread with user priority 20 can set its preemption threshold to 15. The scheduler then treats this thread as priority 15 for preemption decisions — threads with priority 15-20 cannot preempt it, but threads 0-14 still can.
/* ThreadX preemption threshold API */UINT tx_thread_preemption_change(TX_THREAD *thread_ptr,UINT new_threshold,UINT *old_threshold);/* Example: control loop at priority 20, threshold 15 */TX_THREAD control_thread;tx_thread_create(&control_thread, "Control", control_entry, 0,stack, STACK_SIZE, 20, 20, TX_NO_TIME_SLICE, TX_AUTO_START);/* After creation, raise threshold to block medium-priority peers */UINT old_thresh;tx_thread_preemption_change(&control_thread, 15, &old_thresh);
Key behaviors:
new_threshold must be ≤ thread’s user priority (numerically lower or equal = higher priority). Setting 25 on a priority-20 thread returns TX_THRESH_ERROR.TX_DISABLE_PREEMPTION_THRESHOLD is defined at build, the feature is compiled out — all thresholds become 0 (disable all preemption).Priority inheritance is a mutex property. When a high-priority thread blocks on a mutex owned by a lower-priority thread, the owner temporarily inherits the waiter’s priority. This prevents intermediate threads from preempting the owner and extending the inversion.
/* Mutex creation with priority inheritance */TX_MUTEX sensor_mutex;tx_mutex_create(&sensor_mutex, "SensorMutex", TX_INHERIT);/* High-priority task (prio 5) waits, low-priority owner (prio 15) inherits 5 */void high_priority_task(void) {tx_mutex_get(&sensor_mutex, TX_WAIT_FOREVER); /* Blocks, owner boosted to 5 *//* ... critical section ... */tx_mutex_put(&sensor_mutex); /* Owner restored to 15 */}
Key behaviors:
tx_mutex_get() with a wait option.tx_mutex_put().TX_MUTEX_ENABLE_PRIORITY_INHERITANCE defined at build.Here’s the trap: priority inheritance can override your preemption threshold.
ThreadX implements this by setting the thread’s actual preemption threshold to the minimum of:
/* From ThreadX source: tx_thread_preemption_change.c */if (new_threshold < thread_ptr->tx_thread_inherit_priority) {thread_ptr->tx_thread_preempt_threshold = new_threshold;} else {thread_ptr->tx_thread_preempt_threshold = thread_ptr->tx_thread_inherit_priority;}
Scenario:
This is correct behavior (the high-priority waiter must not be delayed), but it defeats your threshold protection during the inversion. If your design assumes the threshold holds during mutex ownership, you have a bug.
+------------------------------------------------------------------+| IS THERE A SHARED RESOURCE? || (mutex-protected) |+------------------------+--------------------------+--------------+| |YES NO| |v v+------------------------------------------------------------------+| Use PRIORITY INHERITANCE on the mutex || + Bounds inversion to mutex hold time || + Works with dynamic priority relationships || + No time-slicing impact |+------------------------------------------------------------------++------------------------------------------------------------------+| DOES A SPECIFIC THREAD NEED PROTECTION FROM || A KNOWN SET OF MEDIUM-PRIORITY PEERS? |+------------------------+--------------------------+--------------+| |YES NO| |v v+------------------------------------------------------------------+ +--------------------------------------------------+| Use PREEMPTION THRESHOLD on that thread | | Standard priority assignment + || + Blocks ONLY the configured range | | proper blocking (vTaskDelay, semaphores) || + Deterministic, no mutex required | | is usually sufficient || - Disables time-slicing | +--------------------------------------------------++------------------------------------------------------------------+
System:
Problem: Logging (15) and Telemetry (12) occasionally preempt Motor Control (10) during its critical math section, causing deadline misses.
Wrong fix: Put a mutex around the math section with priority inheritance.
Right fix: Preemption threshold on Motor Control at 11.
TX_THREAD motor_thread;tx_thread_create(&motor_thread, "Motor", motor_entry, 0,motor_stack, STACK_SIZE, 10, 10, TX_NO_TIME_SLICE, TX_AUTO_START);UINT old;tx_thread_preemption_change(&motor_thread, 11, &old); /* Block 11-31 */
/* In tx_user.h or build config */#define TX_THREAD_ENABLE_PERFORMANCE_INFO#define TX_MUTEX_ENABLE_PERFORMANCE_INFO#define TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO
Rebuild ThreadX library with these defined.
Run the system under the failure condition. In TraceX:
Look for:
| Counter | What It Tells You |
|---------|-------------------|
| priority_inversions (deterministic) | High-priority thread blocked on mutex owned by lower-priority thread |
| priority_inversions (nondeterministic) | Intermediate threads ran during the above wait — this is the killer |
| service_call_preemptions on mutex owner | Owner being preempted while holding mutex |
| interrupt_preemptions | ISR load affecting thread timing |
Switch to the Timeline View. Filter for your high-priority thread and the suspected mutex owner.
Priority inversion signature:
Preemption threshold fix signature:
/* Runtime check in your monitor task */ULONG resumptions, suspensions, preemptions, inversions;tx_thread_performance_info_get(&high_prio_thread,&resumptions, &suspensions,&preemptions, &interrupt_preemptions,&inversions, &time_slices,&relinquishes, &timeouts,&wait_aborts, &last_preempted_by);if (inversions > baseline) {/* Alert: priority inversion rate increased */}
ThreadX SMP (Symmetric Multi-Processing) adds complexity to preemption threshold and priority inheritance. On multi-core systems, a thread can run on any available core, and the scheduler must coordinate across cores.
Key SMP behaviors:
Preemption threshold is per-thread, not per-core. When a thread with threshold 15 runs on Core 0, threads of priority 15-20 on Core 1 can still preempt their own cores but cannot migrate to preempt the threshold-protected thread. The threshold only prevents preemption of that specific thread instance.
Priority inheritance migrates with the thread. If a high-priority thread on Core 1 blocks on a mutex owned by a thread on Core 0, the owner inherits the waiter’s priority on Core 0. The scheduler may migrate the boosted owner to a different core if it improves scheduling latency.
Cross-core preemption has overhead. When a thread’s effective priority changes (via inheritance or threshold), ThreadX may send an inter-processor interrupt (IPI) to the core running the affected thread. This adds microseconds of latency — measurable in TraceX as interrupt_preemptions on the target core.
SMP configuration for debugging:
/* In tx_user.h for SMP builds */#define TX_SMP_ENABLE#define TX_MAX_CORES 4 /* Match your hardware *//* Enable per-core performance tracking */#define TX_THREAD_ENABLE_PERFORMANCE_INFO#define TX_MUTEX_ENABLE_PERFORMANCE_INFO
TraceX SMP analysis:
priority_inversions split by core — inversions on different cores may have different root causesservice_call_preemptions per core to see which core handles mutex contentionPractical SMP guideline: Assign threads with preemption threshold to specific cores using tx_thread_core_affinity_set() if their threshold protection must not be affected by cross-core scheduling decisions. This prevents a medium-priority thread on another core from indirectly affecting the threshold thread via cache contention or memory bus saturation.
Beyond inversion counts, TraceX can quantify the exact cost of your synchronization choices.
Measuring mutex vs threshold overhead:
Compare these metrics:
| Metric | Mutex + Inheritance | Preemption Threshold |
|---|---|---|
| Context switches per second | Higher (block/wake cycles) | Lower (no blocking) |
service_call_preemptions | Elevated on owner | Near zero on protected thread |
interrupt_preemptions | IPIs for inheritance | None from threshold |
| Worst-case latency (µs) | Mutex hold + inheritance | Threshold check only |
TraceX measurement steps:
Typical results on Cortex-M7 @ 480 MHz:
This data justifies threshold for high-frequency control loops where mutex overhead exceeds the jitter budget.
/* WRONG: thread priority 20, threshold 25 (lower priority number = higher priority) */tx_thread_preemption_change(&thread, 25, &old); /* Returns TX_THRESH_ERROR *//* CORRECT: threshold must be <= user priority (numerically) */tx_thread_preemption_change(&thread, 15, &old); /* OK: 15 <= 20 */
/* In tx_user.h */#define TX_DISABLE_PREEMPTION_THRESHOLD/* All tx_thread_preemption_change calls return TX_FEATURE_NOT_ENABLED */
Fix: Remove TX_DISABLE_PREEMPTION_THRESHOLD and rebuild ThreadX library.
If your thread at priority 20 with threshold 15 shares priority with peers, they will not time-slice. The thread runs until it blocks. If it has a tight loop without blocking, it starves peers at same priority.
Priority inheritance only works on mutexes (TX_MUTEX), not semaphores (TX_SEMAPHORE) or event flags. Using a semaphore for mutual exclusion gives you no inheritance — the classic unbounded inversion.
As shown earlier, a thread with threshold 15 that owns a mutex with inheritance will see its effective threshold drop to the waiter’s priority (e.g., 5). If your timing analysis assumes threshold 15 holds during the critical section, you’ve underestimated worst-case preemption.
| Check | Method | Pass Criteria |
|---|---|---|
| Threshold set correctly | tx_thread_preemption_change() returns TX_SUCCESS | old_threshold matches expectation |
| No build-time disable | Search TX_DISABLE_PREEMPTION_THRESHOLD in build config | Not defined |
| Mutex has inheritance | tx_mutex_create(..., TX_INHERIT) | Not TX_NO_INHERIT |
| TraceX shows bounded inversion | Performance Statistics → nondeterministic inversions = 0 | Zero under load |
| Deadline met under stress | Zyclictest or custom timer with 2× load | Max latency < deadline |
| Time-slicing acceptable | Code review: threshold thread blocks/yields periodically | No infinite loops without blocking |
| Situation | Tool | Why |
|---|---|---|
| Shared resource, dynamic waiters | Priority Inheritance (mutex) | Bounds inversion to hold time, no code changes to threads |
| Known peer set, deterministic protection | Preemption Threshold | Blocks specific priority range, no mutex needed |
| Both needed | Use both, but test interaction | Inheritance overrides threshold during contention — verify with TraceX |
The debugging mantra: Capture the trace, count nondeterministic inversions, apply the minimal fix (threshold or inheritance), re-capture, confirm zero nondeterministic inversions. Don’t guess — TraceX shows you exactly which thread ran when.
Quick Links
Legal Stuff





