And the difference is huge, up to 10 times!

On the 4-core processor, the program below with a low proiritet process runs from 800 ms to 1600, while with a high - was not less than 9 seconds

How to explain this unexpected behavior?

Ps. Changed the priority in the task manager, checked many times in the Release configuration without VS, Windows 10

using System; using System.Diagnostics; using System.Threading; namespace LoadTest { class Test { const int n = 1000; static int count = 0; const int stepCount = 10; static AutoResetEvent are = new AutoResetEvent(false); static ManualResetEvent mre = new ManualResetEvent(false); static AutoResetEvent areEnd = new AutoResetEvent(false); static void Main(string[] args) { while (true) { char action = Console.ReadKey().KeyChar; switch (action) { case 't': Threads(); break; } are.Reset(); mre.Reset(); areEnd.Reset(); } } static void Threads() { Thread[] ts = new Thread[n]; for (int i = 0; i < n; i++) { ts[i] = new Thread(CPU); ts[i].Start(); } are.WaitOne();//все потоки готовы var sw = Stopwatch.StartNew(); mre.Set();//начать расчет areEnd.WaitOne();//расчет окончен sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); } static void CPU(object i) { Interlocked.Increment(ref count); if (count == n) are.Set();//оповещаем что все потоки созданы mre.WaitOne();//ждем создания всех потоков Simple(); Interlocked.Decrement(ref count); if (count == 0) areEnd.Set();//оповещаем о завершении всех потоков } static void Simple() { for (int j = 0; j < stepCount; j++) Console.WriteLine(j); } } } 
  • I suspect that the plug in the Windows scheduler - Vladislav Khapin
  • @VladislavKhapin, it seems to me that I have to dig in the direction of "many streams are being output to one console" - Qutrix
  • one
    if it comes to real-time, then there is such a thing in the book of the Windows Internals: system functions (such as the memory manager, cache manager, or other device drivers). Because of this, a program that generates many streams with a real-time priority may work more slowly , since it takes up all the airtime from system streams. But this is speculation. - Vladislav Khapin
  • @VladislavKhapin, I just don’t think that I use this code to run these very "important Windows kernel-mode system threads" - Qutrix
  • @Quitrix does not say that you are running them. It says that system streams work in real-time mode. If you run the process in question in real-time mode, then all its threads will work with real-time priority, and these threads may interfere with the work of the Windows system functions, which will cause a drop in performance. - Vladislav Khapin

0