-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Our C++17 parallel algorithms use the Windows threadpool:
Line 213 in 165fc94
| : _Ptp_work(::__std_create_threadpool_work(&_Work::_Threadpool_callback, _STD addressof(_Operation), nullptr)) { |
STL/stl/src/parallel_algorithms.cpp
Lines 23 to 26 in 165fc94
| [[nodiscard]] PTP_WORK __stdcall __std_create_threadpool_work( | |
| PTP_WORK_CALLBACK _Callback, void* _Context, PTP_CALLBACK_ENVIRON _Callback_environ) noexcept { | |
| return CreateThreadpoolWork(_Callback, _Context, _Callback_environ); | |
| } |
We're passing null to CreateThreadpoolWork for the callback environment:
If this parameter is NULL, the callback executes in the default callback environment. For more information, see
InitializeThreadpoolEnvironment.
Therefore, the threadpool callbacks have default priority, regardless of the parent thread priority.
Think-cell reported this to us, raising the concern of priority inversion.
In theory, we could alter our code to set up a callback environment, and use SetThreadpoolCallbackPriority to request low, normal, or high priority. However, it is unclear to me whether "The priority for the callback relative to other callbacks in the same thread pool." would control the priority relative to the parent thread, which is what would be needed to address priority inversion concerns.
Ultimately, the conclusion may be that thread priority is outside the realm of the Standard, so if an application has used the Windows API to alter its thread priorities, then the C++17 parallel algorithms are a poor fit for it.