title: Thread priority at Windows, have no matters?
body: <p>I was getting fun with threads priority and affinity. What I was doing that, I saw at least one problem - when I use only one CPU, threads are not getting enought processor time. For example I have here some result of my observation.</p>
  
  <p><a href="https://pastebin.com/2fgPwMbw" rel="nofollow noreferrer">https://pastebin.com/2fgPwMbw</a></p>
  
  <p>First column is time in ms, second one is a counter when thread have priority equal 0, third and fourth is counter when priority is -1, and fifth and sixth is counter when priority is -2.
  And It is my code:</p>
  
  <pre><code>DWORD WINAPI thread( void* counter )
  {
  DWORD* local = ( DWORD* )counter;
  
      while (1)
      {
          for (int i = 0; i &lt; 5000; i++)
          {
              suma += sin( i );
          }
          ++(*local);
          suma = 0;
      }
  }
  
  
  int main( )
  {
      DWORD_PTR affinity = 1;
  
      SetProcessAffinityMask( GetCurrentProcess( ), affinity );
  
      LPDWORD counters = ( LPDWORD )calloc( THREADS, sizeof( DWORD ) );
  
      int normalPriorityIndex = 0;
      int lowPriorityIndex[2] = { 1, 2 };
      int idlePriorityIndex[2] = { 3, 4 };
  
      HANDLE hw[THREADS] = {
          CreateThread( 0, 0, thread, counters + normalPriorityIndex, 0, 0 ),
          CreateThread( 0, 0, thread, counters + lowPriorityIndex[0], 0, 0 ),
          CreateThread( 0, 0, thread, counters + lowPriorityIndex[1], 0, 0 ),
          CreateThread( 0, 0, thread, counters + idlePriorityIndex[0], 0, 0 ),
          CreateThread( 0, 0, thread, counters + idlePriorityIndex[1], 0, 0 ),
      };
  
      SetThreadPriority( hw[normalPriorityIndex], 0 );
      SetThreadPriority( hw[lowPriorityIndex[0]], -1 );
      SetThreadPriority( hw[lowPriorityIndex[1]], -1 );
      SetThreadPriority( hw[idlePriorityIndex[0]], -2 );
      SetThreadPriority( hw[idlePriorityIndex[1]], -2 );
  
      FILE* file;
      char* fileName = ( char* )calloc( 32, sizeof( char ) );
      fileName = "dane.txt";
      fopen_s( &amp;file, fileName, "w" );
  
      DWORD startTime = GetTickCount( );
      FILE* stream = stdout;
  
      while (!_kbhit( ))
      {
          if (GetTickCount( ) - startTime &gt; 600000)
          {
              break;
          }
          fprintf( file, "%5.ld  |  ", GetTickCount( ) - startTime );
          fprintf( file, "%ld  %ld  %ld  %ld  %ld\n",
              counters[normalPriorityIndex], 
              counters[lowPriorityIndex[0]],
              counters[lowPriorityIndex[1]],
              counters[idlePriorityIndex[0]], 
              counters[idlePriorityIndex[1]] );
          Sleep( 750 );
      }
  
      for (int i = 0; i &lt; THREADS; i++)
          CloseHandle( hw[i] );
  
      return 0;
  }
  </code></pre>
  
  <p>Why threads with priority -2 have bigger counter, then thread with priority -1...?</p>