Convoying and priority inversion

Convoying is a problem that comes up between threads of the same priority and is a concern when using locks and Threading Building Blocks. Convoying occurs when the operating system interrupts a task that is holding a lock. All other tasks must wait until the interrupted task resumes and releases the lock. Fair mutexes can make the situation even worse because if a waiting task is interrupted, all the tasks behind it must wait for it to resume.

To avoid convoying, use atomic operations instead of locks where possible.

Convoying is similar to priority inversion. Priority inversion describes a general case where a lower-priority task holds a shared resource that is required by a higher-priority task. The higher-priority task is therefore blocked until the lower-priority task has released the resource, effectively inverting the relative priorities of the two tasks.

To minimize convoying and priority inversion, use atomic operations if possible. Otherwise, hold any lock as briefly as possible. Precompute whatever you can before acquiring the lock.

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.