Note that there are some explanatory texts on larger screens.

plurals
  1. POProcess and thread priority in windows 7
    primarykey
    data
    text
    <p>I have a simple "training" project wich must show the priority mechanism in Windows.</p> <p>This is my C++ code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;windows.h&gt; DWORD WINAPI Thread1(LPVOID); int stop; int sleep = 10000; struct params { int num; bool* runflg; }; long long counters[7] = {0,0,0,0,0,0,0}; int priority[7] = {THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL}; int main(int argc, char* argv[]) { int thrds; if (argc &lt; 2) stop = 5; else stop = atoi(argv[1]); bool runFlag = true; __int64 end_time; LARGE_INTEGER end_time2; HANDLE tm1 = CreateWaitableTimer(NULL, false, NULL); end_time = -1 * stop * 10000000; end_time2.LowPart = (DWORD) (end_time &amp; 0xFFFFFFFF); end_time2.HighPart = (LONG) (end_time &gt;&gt; 32); SetWaitableTimer(tm1, &amp;end_time2, 0,NULL, NULL, false); //SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); printf("process priority = %d \n", GetPriorityClass(GetCurrentProcess())); SetProcessPriorityBoost(GetCurrentProcess(), true); for (int i = 0; i &lt; 7; i++) { DWORD targetThreadId; params* param = (params*)malloc(sizeof(params)); param-&gt;num = i; param-&gt;runflg = &amp;runFlag; HANDLE t1 = CreateThread(NULL, 0, Thread1, param, 0, &amp;targetThreadId); SetThreadPriority(t1, priority[i]); //задание приоритета PBOOL ptr1 = (PBOOL)malloc(sizeof(BOOL)); GetThreadPriorityBoost(t1, ptr1); SetThreadPriorityBoost(t1, true); //запрет динамического изм. приоритета CloseHandle(t1); } WaitForSingleObject(tm1,INFINITE); runFlag = false; CloseHandle(tm1); printf("\n"); for (int i = 0; i &lt; 7; i++) { printf("%d - %ld\n",i, counters[i]); } return 0; } DWORD WINAPI Thread1(LPVOID prm) { params arg = *((params*)prm); printf("thread # %d priority = %d \n", arg.num, GetThreadPriority(GetCurrentThread())); while(1) { counters[arg.num]++; Sleep(0); if(*(arg.runflg) == false) break; } return 0; } </code></pre> <p>In the code, I create 7 threads with different thread priorities. Every thread has its own counter. The program should run for about 5 seconds, and after that the console must show the threads' priorities and their values. When I did it a year ago on Win XP 32 everything was working - a thread with less priority has a smaller counter value. But now I have strange results like this:</p> <pre><code>process priority = 32 thread # 0 priority = -15 thread # 1 priority = -2 thread # 2 priority = -1 thread # 3 priority = 0 thread # 4 priority = 1 thread # 5 priority = 2 thread # 6 priority = 15 0 - 5401405 1 - 5726804 2 - 6676367 3 - 8320768 4 - 3223481 5 - 3085247 6 - 3177885 </code></pre> <p>Why are priority levels not working and counters have such strange values (not sorted ascending)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload