3

Is there any way, other than to disable half of the cores using SetThreadAffinityMask(), to make a hyper-threading CPU fully obey thread priorities?

AFAIK, the CPU itself is not aware of priorities, so a low priority thread running on core 1 will be given roughly an equal amount of execution time as a high priority thread running on core 0. In other words, the low priority thread causes the high priority thread to complete its work (potentially much) later.

My assumption here is that core 0 and 1 are two logical CPUs, located on the same physical CPU.

According to comment by Hans, this boils down to if Intel HT CPU supports thread priorities in hardware. Do they?

Sven Nilsson
  • 1,861
  • 10
  • 11
  • There should be an option to disable hyperthreading in BIOS. – themagicalyang Nov 02 '16 at 10:29
  • Yeah, that's about the same as disabling half of the cores with SetThreadAffinityMask. – Sven Nilsson Nov 02 '16 at 10:30
  • 4
    Is there any particular reason you're not willing to trust the OS to prioritise threads appropriately? You've instructed the OS to make a particular thread low priority. The OS is capable of recognising a hyper-threading CPU. Surely it's up to the OS to time-slice and distribute threads across available cores according to its prioritisation rules? (_NOTE: The OS has to manage distribution of **all** threads from **all** processes._) It seems to me, trying to second-guess the OS just leads down a slippery platform-specific slope. – Disillusioned Nov 02 '16 at 10:50
  • 1
    It is not about trust, but rather my observation that the presence of the low priority thread slows down the high priority thread. The high prio thread finishes after 103 seconds and the low prio one after 107 seconds. If I remove the low priority thread, the high priority thread finishes after 66 seconds. – Sven Nilsson Nov 02 '16 at 11:09
  • 2
    Before asking if we trust the operating system a more fundamental question is: does the CPU support this? Normally prioritizing threads is the task of the OS, but if there are threads of different priority running on a hyper-threaded CPU the CPU needs to be able to priority between them - so that "hyperthread 0" should preempt "hyperthread 1" in some way. Similarly if two cores share a cache we might want to give one core priority to the cache. I don't know if current Intel CPUs can do that. Thus I would go with the SetThreadAffinityMask – Hans Olsson Nov 02 '16 at 12:44
  • Thanks, updated question based on this – Sven Nilsson Nov 02 '16 at 13:08
  • Most PCs allow you to turn off hyperthreading in the firmware. – Harry Johnston Nov 02 '16 at 21:59
  • @SvenNilsson Given that you have some bench-marking code, perhaps it would serve to include that in your question; along with your findings? – Disillusioned Nov 02 '16 at 23:53
  • Thanks, actually it does not help in this case. The benchmark is correct (I have thousands of hours of multithreading experience) and now I just need someone who is an Intel CPU expert to say if these processors have some kind of support for priorities in the hardware. I Googled the Intel website quite a lot and found nothing so far. If there is support in the hardware, some future version of Windows may fix this 'priority inversion' issue. – Sven Nilsson Nov 03 '16 at 09:37
  • @SvenNilsson "_actually it does not help in this case. The benchmark is correct (I have thousands of hours of multithreading experience)_" sounds like a defensive cop-out. I wasn't questioning your multi-threading experience. Simply suggesting that if you want someone to help experiment with ideas, using your ***existing*** benchmark would be useful (rather than expecting them to try to reinvent something similar but probably not quite the same). I cannot fathom why you wouldn't want to show your code - regardless of whether you overlooked something, it can ***only help***! – Disillusioned Nov 03 '16 at 10:59
  • A bit late to the party, but this isn't just a hyperthreading issue. With dynamic throttling present on most modern CPUs, even threads running on different physical cores slow each other down. So I guess the only way of preventing this kind of "priority inversion" is to disable hyperthreading and set a fixed clock rate. For instance, this is what Beckhoff does on their industrial PCs made for running real-time PLC and NC systems. – relatively_random Dec 28 '18 at 15:02

0 Answers0